So guy's i'm trying to validate the persons input, if the person types a number as the answer for (y/n) then it'll go back to main and tell you that you did not put in the correct thing, however if someone where to put in yn at the same time then the code will repeat itself over and over again. Any help would be appreciated thank you :)
#include <iostream>
usingnamespace std;
int main()
{
int x;
char y;
x = 0;
cout << "Input number then press enter: " << endl;
cin >> x;
cout << "You input the number: " << x << " correct? (y/n then press enter) ";
cin >> y;
if (y == 'y')
{
cout << "Good this worked";
}
elseif (y == 'n')
{
cout << "Guess I didn't get the right number";
main();
}
else
{
cout << "Progam failed or you did not put in the correct choice" << endl;
main();
}
return 0;
}
So now how would i go about clearing what is stored in the char, ex. i'll put .5 and the program will output try again, try again. Then even if i were to put y or n it'll still give me the try again, so i'm thinking i'd have to clear the cache stored in char y; any thoughts?
Thank you, eyenrique. Took me a little bit of researching to figure out what you did :) ( haven't taught myself bools yet). Im not suer as to how the cin.fail works though, can you explain that to me a little?
In simple words if
your input fails, cin.fail() then
member function "clear" is
used to restore your input
stream to "a good state"; cin.clear();
and cin.ignore(); ignores the last 1000
characters entered plus new lines cin.ignore(1000,'\n');
For a more detailed explanation
read about C++ stream Input/Output
Error states;
regards!
P.S.
sorry for the late reply i was really sick :S