Sorry for this extreme noob question,
I am using a for loop with 5 iterations. Each time it loops I prompt the user to enter a decimal, I use cin >> aDouble. If I type in any number it works fine, but if I type in a string it will loop the five times, then go on.
Here’s my code.
for(int i = 0; i <= ARRSIZE; i++)
{
cout << "Please enter a decimal value: ";
cin >> myDouble;
if (!cin.fail() )
{
myVector.push_back(myDouble);
}
}
Thank you!!
Once a stream failed to read a value it sets
std::ios_base::failbitand this bit stays set until it gets cleared. While an error bit is set, the stream doesn’t do anything. That is, ifcin.fail()istrueyou deal with the problem as well: You need to get rid of the “string”: