Reputation: 57
The following code produces an infinite loop when I enter 2 cstrings of entirely 1s and 0s.
What have I done?
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);
Upvotes: 1
Views: 63
Reputation: 57
This got it. For some reason it didn't like the ignores, and terminating the cin.getline functions with null characters was creating the infinite loop.
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);
Upvotes: 1