有关二进制转化为十进制的问题——出了点小毛病,望高手指点。
#include<iostream>
using namespace std;
int main()
{
int sum=0,num,i=1;
cout<<"Input a binary number:"<<endl;
cin>>num;
while(num>0)
{
if(num%10>1)
{cout<<"Error input!"<<'\n';
goto loop;//第一个goto语句
}
sum+=(num%10)*i;
i*=2;
if(sum>1023)//二进制1111111111=1023
{
cout<<"The number is too large!"<<endl;
goto loop;//第二个goto语句
}
num/=10;
}
cout<<"the decimal notation of the binary number is :"<<endl;
cout<<sum<<endl;
loop:
return 0;
}
当输入的二进制数高于十位时,输出结果恒为0。第二个goto语句防止它高于十位,可是不起作用。
请高手告知为什么高于十位时结果恒为0,并请改进该程序使之对高于十位的情况发出警告