一段关于十六进制转换的C++代码,没找到错误在哪
本帖最后由 yzyou521 于 2016-2-22 15:02 编辑如题:编译没有问题,运行结果是错误的
void GetNumber(char CharNum[])
{
int i=0;
int Figure;
int HexDencimalNumber;
//开始十六进制获取数据
cout<<"Please input a hexdecimal number."<<endl;
cout<<"Then ending with 'H' or 'h'.:";
cin>>CharNum;
cout<<"Hexdicimal list like this:"<<endl;
while(CharNum!='h' && CharNum!='H')
{
//cout<<CharNum<<endl; 临时测试语句
if(CharNum='0')
{
HexDencimalNumber=0;
}
if(CharNum=='1')
{
HexDencimalNumber=1;
}
if(CharNum=='2')
{
HexDencimalNumber=2;
}
if(CharNum=='3')
{
HexDencimalNumber=3;
}
if(CharNum=='4')
{
HexDencimalNumber=4;
}
if(CharNum=='5')
{
HexDencimalNumber=5;
}
if(CharNum=='6')
{
HexDencimalNumber=6;
}
if(CharNum=='7')
{
HexDencimalNumber=7;
}
if(CharNum=='8')
{
HexDencimalNumber=8;
}
if(CharNum=='9')
{
HexDencimalNumber=9;
}
if(CharNum=='a'||CharNum=='A')
{
HexDencimalNumber=10;
}
if(CharNum=='b'||CharNum=='B')
{
HexDencimalNumber=11;
}
if(CharNum=='c'||CharNum=='C')
{
HexDencimalNumber=12;
}
if(CharNum=='d'||CharNum=='D')
{
HexDencimalNumber=13;
}
if(CharNum=='e'||CharNum=='E')
{
HexDencimalNumber=14;
}
if(CharNum=='f'||CharNum=='F')
{
HexDencimalNumber=15;
}
i++;
}
Figure=i;
cout<<Figure<<endl;//test function输出正确
cout<<"Change to number is:"<<endl;
for(i=0;i<Figure;i++)
{
cout<<HexDencimalNumber<<endl; //此处的输出结果是错误的
}
} 自己找到错误了 if(CharNum='0') 少了个等号 ,if(CharNum=='0')
页:
[1]