Basically, in this code, after it loops around at least once in the end, because you haven't written either yes or no, it keeps skipping the first letter of what you've written. For example: (go to bottom)
#include <iostream>
#include <string>
#include <limits>
usingnamespace std;
int searcher(int search[], int size, int value)
{
int low = 0;
int high = size - 1;
while (low <= high)
{
int mid = (high + low) / 2;
if (value == search[mid])
{
return mid;
}
elseif (value > search[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int toSearch[8] = { 12, 22, 34, 47, 55, 67, 82, 98 };
while (1 == 1)
{
cout << endl << "What number do you want to search?: ";
int num = 0;
while (!(cin >> num))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << "Error! You either didn't enter a number or your number was too long!" << endl << endl << "Retry: ";
}
int result = searcher(toSearch, 8, num);
if (result >= 0)
{
cout << endl << endl << "The number \"" << num << "\" was located in the array at index \"" << result << "\"" << endl;
}
else
{
cout << endl << "The number \"" << num << "\" was not found in the array..." << endl << endl;
}
string choice = "";
int stupidity = 0;
do
{
cout << endl << endl << "Restart program?: ";
cin.ignore();
getline(cin, choice);
if (choice == "yes")
{
}
elseif (choice == "no")
{
return 0;
}
else
{
cout << endl << endl << "Type either \"yes\" or \"no\"" << endl;
stupidity++;
if (stupidity >= 10 && stupidity < 12)
{
cout << endl << "How many times are you gonna make a mistake here!?" << endl;
}
elseif (stupidity >= 21 && stupidity < 23)
{
cout << endl << "Just say either \"yes\" or \"no\"!";
}
}
} while (choice != "yes" && choice != "no");
}
system("pause");
return 0;
}
First let it run normally, then let it ask you for the 1st time whether or not you want to restart, type something, anything, but make it sure it's at least 2 words separated with a space or anything like that. Normally, it goes to the ELSE part, does those things, then goes back around, but this time, no matter what you write, it won't accept it.
Or to make it more clearly, here are some steps to reproduce the problem:
1 - run program
2 - where it asks you for a number, type any number
3 - where it asks you to restart, type for example "yes and no" without the quotes
And after you've written at least 2 words at the end, the program bugs out and when it asks you again, no matter what you write, it won't accept it, because at the part where it says
1 2
cin.ignore();
getline(cin, choice);
it always skips the first letter of what you've written.
Actually, I think I've fixed it by making an IF statement, where the 1st time it goes around normally, but the second time it doesn't do cin.ignore() by making it like this:
string choice = "";
int stupidity = 0;
int protection = 0;
do
{
cout << endl << endl << "Restart program?: ";
if (protection == 0)
{
cin.ignore();
getline(cin, choice);
protection++;
}
else
{
getline(cin, choice);
}
if (choice == "yes")
{
}
elseif (choice == "no")
{
return 0;
}
else
{
cout << endl << endl << "Type either \"yes\" or \"no\"" << endl;
stupidity++;
if (stupidity >= 10 && stupidity < 12)
{
cout << endl << "How many times are you gonna make a mistake here!?" << endl;
}
elseif (stupidity >= 21 && stupidity < 23)
{
cout << endl << "Just say either \"yes\" or \"no\"!";
}
}
} while (choice != "yes" && choice != "no");
But does anyone know a better way to prevent this bug? I don't even know why it's caused, I only know how it works - the 2nd time it goes around the "do while" loop, due to cin.ignore(), it ignores the first letter of whatever you've written, but I don't know why it doesn't do that the first time. So far this is the only way I've found to prevent this. But is there a better way?
At the very beginning, there's a small bug, where, if you write 2 numbers instead of one, separated by a space or anything like that, it takes the first number and uses it at the beginning, but it also keeps the second and uses it at the next opportunity. How do I make it so that the very first cin gets the first number? (Not digit, but number. For example, if you write "12 24" without the quotes, how do I make it only accept the first "12"?
If there's no answer, which I wouldn't blame, since now the title doesn't completely meet with the current question, I could make the question in the previous post into it's own topic.
For your original question, you could have just moved the cin.ignore(); to before your do while loop. If you want to know why you needed it in the first place, it's because after you read in the number, the '\n' is left in the stream. getline(cin, choice); will read in characters to the string until it hits '\n'. So if you don't ignore that '\n', you would be storing an empty string in choice.
For your second question, I've seen LB link an article he wrote that I think answers it pretty well.