Switch Structure Do while Loop

I have a switch structure here. Suppose the user enters in a default value. Well I want the user to try it again. I have contructed a do while loop but cant seem to figure out what to put in while. I was thinking of while(its is default), but not knowingly how :(

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
 void choiceletter(char &choice)
{
	cout<<"\n\nSelect Logo: ";
	//Use a do while loop. Keep reapting the loops until the user enters in a valid entry.
	do
	{
	cin>>choice;
	
	
	//If the user enters in a lower case alphabet it will capatilize it
	choice=(char)toupper(choice);

	
	//Do this switch case until user enters a correct entry
	
	switch (choice)
	{
	case 'A':
		break;
	case 'B':
		break;
	case 'C':
		break;
	case 'D':
		break;
	case 'E':
		break;
	default:
		cout<<"\n\nInvalid Entry!";
		

	
	}
	}
	while ();
Use a boolean value. You can have it be true at the beginning of your loop and set it false in your default case.
Do you need the switch cases to do anything, or are you just trying to validate the entry?

For the first case, I would typically have a 'quit' option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getChoice( char& choice )
{
  do
  {
    cin >> choice;

    switch ( choice )
    {
      case 'a': doSomething(); break;
      case 'q': break;
      deafult:  cout << "Not Valid\n";
    }
  } while( choice != 'q' );
}


For the second, create a new function that checks validity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool validChoice( const char choice )
{
  static const char *valid_choices = "abcedf";

  const char *ptr = valid_choices;
  while ( *ptr && *ptr != choice ) ptr++;

  return *ptr == 0;
}

void getChoice( char& choice )
{
  do
  {
    cin >> choice;
  } while ( !validChoice( choice ) );
}


If you prefer the first, I would suggest changing the name of the function, because it does more than get a choice.
Last edited on
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
void choiceletter(char &choice)
{
	//Use a do while loop. Keep reapting the loops until the user enters in a valid entry.
	
	bool invalid_choice = false;
	do
	{
		cout<<"\n\nSelect Logo: ";
		cin>>choice;
		
		//If the user enters in a lower case alphabet it will capatilize it
		choice=(char)toupper(choice);
		
		//Do this switch case until user enters a correct entry
		invalid_choice = false;
		
		switch (choice)
		{
		case 'A':
			break;
		case 'B':
			break;
		case 'C':
			break;
		case 'D':
			break;
		case 'E':
			break;
		default:
			invalid_choice = true;
			cout<<"\n\nInvalid Entry!";
		
		}
	} while (invalid_choice);
}
I'm going to try all the methods out to become a little expertise on it. I liked tipyae idea so far, but I was wondering( can you translate what while(invalid_choice) means in the parenthesis. A whole English translation. for instance, i'm assuming while invalid choice is true perform the do while loop.
When you have a construct
1
2
3
4
do
{

} while (condition)


The instructions in the do block are executed
After the end of the do block, the condition is tested
As long as the condition is true the do block is executed again, and the condition tested again.
This will continue until the condition is false.
The condition can be any expression. If the expression yields false or zero, then the condition is false.
Any other result and the condition is true.

invalid_choice is of type bool, which means it can only have one of two values, either true or false.
Before the switch, invalid_choice is set to false. Inside the switch block, if the default case is executed, then invalid_choice is set to true. When it is tested after the block the result is therefore true- that means the do block will be executed again.
If any of the valid choices are picked, then nothing changes the value of invalid_choice from false, so when it is tested after the do block it is found false, and the do-while loop ends.

Check out the do-while section on this page: http://www.cplusplus.com/doc/tutorial/control/
thank you tipaye and the rest of the users who offered their insight on my problem. Highly appreciated and surely did gain some notes of Boolean tehcniques.
Topic archived. No new replies allowed.