Input Validation

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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>

using namespace 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";
    }

    else if (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;
}
1
2
3
4
5
6
while(y!='y' || y!='n')
{
    cout<<"Try Again\n";
    cin>>y;
}
//do stuff with correct input 
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?
@RadWayne
 
while(y!='y' || y!='n')

That while statement will always be true. You want an AND condition, not an OR condition.

Hi @Relit

I guess that is not a good practice calling
the function "main" recursively;

This is an iterative example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//inputChar.cpp
//##

#include <iostream>


using namespace std;



int main(){

bool repeat=true;
int x;
char y;

while(repeat){

        cout<<"Input number: ";
        cin>>x;

        while(cin.fail()){
                cin.clear();
                cin.ignore(1000,'\n');
        cout<<"\nInput number: ";
        cin>>x;
        }//end while

        cout<<"You entered: "<<x<<" (y/n) ";
        cin>>y;

        while(!(y=='y'||y=='n')||cin.fail()){ //or while((y!='y'&&y!='n')||cin.fail())
                if(cin.fail()){
                        cin.clear();
                        cin.ignore(1000,'\n');
                }//end if
        cout<<"\nYou entered: "<<x<<" (y/n) ";
        cin>>y;
        }//end while

        if(y=='y'){
                cout<<"\nGood this worked!"<<endl;
                repeat=false;
        }else{

                cout<<"Guess I didn't get the right number"<<endl;
        }//end if-else

 }//end while
}//end of main 




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


Topic archived. No new replies allowed.