If/Else loop input validation

Hello all:

I'm trying to figure out input validation. The users options are either A,C,He,or H. If they cin a valid input, then I proceed with the switch that I have working.

If they put in an invalid input I'm supposed have them re-enter a value till they get it right.

Anyway how do I return to the initial prompt after they fail the first time?

Consider the only loops we have learned so for are for loops, so whiles are unavailable.

Is there someway to return to the top after the default output is displayed in the switch.

I figured if/elses won't work as that's what I've been trying:

1
2
3
if((selection == 'A') || (selection == 'C')||(selection == 'He')||(selection == 'H' )){

		switch (selection)


but how do I return to the initial prompt in the else{} statement?

If you do a for loop?:

1
2
3
for ((selection == 'A') || (selection == 'C')||(selection == 'He')||(selection == 'H' ); ; )

      switch


Basically can I go condition-> true -> processor -> display
condition-> false ->condition

This is the first time I'm using a for loop if I have to use one.

Any help will be great Thanks!!
1
2
if((selection == 'A') || (selection == 'C')||(selection == 'He')||(selection == 'H' )){
		switch (selection)


selection is char type of variable i assume; 'He' is NOT a char. it is either char* or string. Different concepts.

Why not go for switch statelement straight away?
1
2
3
4
5
6
7
8
9
10
11
12
switch (selection) {
  case 'A':
    // Case A code
    break;
  case 'C':
    //case C code
    break;
  // .............
  default: //if above code didn't succeed:
    cout << "There is nothing i can help you with" << endl;
    break;
}
I would love to just end it there with the default, but if the user puts in the wrong input it has to repromt them for the correct input
A for loop is in fact a while loop in disguise, so it's often possible to easily convert one into the other.
In you case, you could create an infinite loop in which you get the user input and only break out of it if the answer is valid.
Using a while loop, it would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string selection;
while( true )
{
    std::cout << "Please enter your selection\n";
    getline(std::cin,selection);
    
    if( (selection==std::string("A")) || (selection==std::string("C")) || (selection==std::string("He")) || (selection==std::string("H")) )
        break;
}

switch( ... )
{
}

Using a for loop, it becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string selection;
for( ; true;  )
{
    std::cout << "Please enter your selection\n";
    getline(std::cin,selection);
    
    if( (selection==std::string("A")) || (selection==std::string("C")) || (selection==std::string("He")) || (selection==std::string("H")) )
        break;
}

switch( ... )
{
}


You could also use a goto instruction, but if you can't use while loops I doubt you can use gotos.

A more elaborate method would be to use a recursive function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void GetSelection( std::string& selection )
{
    std::cout << "Please enter your selection\n";
    getline(std::cin,selection);
    
    if( (selection!=std::string("A")) && (selection!=std::string("C")) && (selection!=std::string("He")) && (selection!=std::string("H")) )
    {
        std::cout << "Your selection is invalid.\n";
        GetSelection(selection);
    }
}

int main()
{
    std::string selection;

    GetSelection(selection);

    // process your data ....

    return 0;
}
Last edited on
this is incredibly helpful thank you kindly
Topic archived. No new replies allowed.