Repeat switch statement c ?

Home » Programming & Design » Repeat switch statement c ?
Programming & Design No Comments

I need to repeat this switch statement until the user enters R,P,S, or Q. So, for instance, if the user types R, it should go to the next function. if they type anything besides the above choices, it should repeat the whole process.

cout << "Make a selection – Rock(R), Paper(P), Scissors(S), or

I need to repeat this switch statement until the user enters R,P,S, or Q. So, for instance, if the user types R, it should go to the next function. if they type anything besides the above choices, it should repeat the whole process.

cout << "Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): ";
cin >> userChoice;
switch (userChoice)
{
case 'r':
case 'R': userChoiceValue = 1;
break;
case 'p':
case 'P': userChoiceValue = 2;
break;
case 's':
case 'S': userChoiceValue = 3;
break;
case 'q':
case 'Q': cout << "The program will now exit" << endl;
exit(0);
break;
default: cout << "Please enter R,P,S, or Q: " << endl;
break;
return 1;
}

Thank you for any help.

Best Answer:

Stephanie: Wrap your switch statement in a while loop like this:

bool active=true;

while(true==active)

//switch statement

Then, if your target letters are entered, set the active flag to false.

Other answer:

Stephanie:
Unless this assignment requires you to use a switch statement, I wouldn't. For most user input validation, I recommend creating a function that does the work. See below for an example. If you want to keep what you have, just wrap it in a while loop:

. userChoiceValue = 0;
. while (userChoiceValue == 0) {
. . // your code (without that unreachable 'return 1;' statement)
. }

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

enum choice_t { ROCK, PAPER, SCISSORS, QUIT, NONE };
const string choiceStr[] = { "Rock", "Paper", "Scissors" };
choice_t getChoice(const string &);

int main(int argc, char *argv[]) {
. . static const string prompt("Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): ");
. . choice_t c;

. . do {
. . . . if ((c = getChoice(prompt)) != QUIT) {
. . . . . . cout << choiceStr[c] << "\n\n";
. . . . }
. . } while (c != QUIT);
. . cout << "bye" << endl;
. . return 0;
}

choice_t getChoice(const string &prompt) {
. . static const string validChoice("rpsq");
. . choice_t choice = NONE;
. . string in;
. . size_t i;

. . do {
. . . . cout << prompt;
. . . . getline(cin, in);
. . . . if ((in.size() >= 1) && ((i = validChoice.find(tolower(in[0]))) != string::npos)) {
. . . . . . choice = static_cast<choice_t>(i);
. . . . }
. . } while (choice == NONE);

. . return choice;
}

#if 0

Sample run:

Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): r
Rock

Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): x
Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): P
Paper

Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): S
Scissors

Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): q
bye

#endif

S:
Use a basic control loop either While or For loop.

bool run = true;
while(run)
{
//do stuff until run = false
}

or

for(int i = 0 ; i < [# OF TIMES TO LOOP] ; i++)
{
//do things;
}

roger:
your code is C++ not C
userChoiceValue=0;
do{
cout << "Make a selection – Rock(R), Paper(P), Scissors(S), or Quit(Q): ";
cin >> userChoice;
switch (userChoice) {
case 'r':
case 'R':
userChoiceValue = 1;
break;
case 'p':
case 'P':
userChoiceValue = 2;
break;
case 's':
case 'S':
userChoiceValue = 3;
break;
case 'q':
case 'Q':
cout << "The program will now exit" << endl;
userChoiceValue=4;
break;
default: cout << "Please enter R,P,S, or Q: " << endl;

}
}while(userChoiceValue!=4);

LEAVE A COMMENT