当我输入完全为1和0的2个cstrings时,以下代码会产生无限循环。
我做了什么?
char input1[9] = {'\0'};
char input2[9] = {'\0'};
bool reEnter = false;
do
{
reEnter = false;
cout << "The numbers to be added are: "<< endl;
cin.ignore();
cin.getline(input1, 9, '\0');
cin.ignore();
cin.getline(input2, 9, '\0');
for (int i = 0; i<8; i++)
{
if((input1[i] != '0') && (input1[i] != '1') || (input2[i] != '0') && (input2[i] != '1'))
{
reEnter = true;
}
}
if(reEnter == true)
cout << "Must be an 8 bit binary" << endl;
}while(reEnter == true);
答案 0 :(得分:1)
这就搞定了。由于某种原因,它不喜欢忽略,并且使用空字符终止cin.getline函数正在创建无限循环。
char input1[9] = {'\0'};
char input2[9] = {'\0'};
bool reEnter = false;
do
{
reEnter = false;
cout << "The numbers to be added are: "<< endl;
cin.getline(input1, 9);
cin.getline(input2, 9);
for (int i = 0; i<8; i++)
{
if((input1[i] != '0') && (input1[i] != '1') || (input2[i] != '0') && (input2[i] != '1'))
{
reEnter = true;
}
}
if(reEnter == true)
cout << "Must be an 8 bit binary" << endl;
}while(reEnter == true);