'Is there a way to keep on looping a switch statement if a user enters anything other than an integer in c++?
My loop works for a scenario where a user enters a number not in the list but when I insert a character or string, the loop does'nt work. I wanted to make it such that it'll keep on looping as long as the switch statement case is default, irregardles of whether its a number or character. I tried choice!=int and choice==string to create a while loop but apparently its not possible.
void userDetails();
int main()
{
userDetails();
return 0;
}
void userDetails()
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname + lastname << flush << endl;
cout << "2. " << lastname + firstname << flush << endl;
cout << "3. " << firstname + "254" << flush << endl;
cout << "4. " << lastname + "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
username = firstname + lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname + firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
while (choice > 5)
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname + lastname << flush << endl;
cout << "2. " << lastname + firstname << flush << endl;
cout << "3. " << firstname + "254" << flush << endl;
cout << "4. " << lastname + "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
cin >> choice;
switch (choice)
{
case 1:
username = firstname + lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname + firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
}
while (choice ==0)
{
cout << "Select a username below:" << endl;
cout << "1. " << firstname + lastname << flush << endl;
cout << "2. " << lastname + firstname << flush << endl;
cout << "3. " << firstname + "254" << flush << endl;
cout << "4. " << lastname + "254" << flush << endl;
cout << "5. Enter other username:" << flush << endl;
cin >> choice;
switch (choice)
{
case 1:
username = firstname + lastname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 2:
username = lastname + firstname;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 3:
username = firstname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 4:
username = lastname + "254";
cout << "You have selected " << username << " as your username" << flush << endl;
break;
case 5:
cout << "Create a username:" << endl;
cin >> username;
cout << "You have selected " << username << " as your username" << flush << endl;
break;
default:
cout << "Invalid option, please try again!!!!";
break;
}
}
}
Solution 1:[1]
I think you can do as below.
- read a string instead of integer.
- parse string to integer.
- if there are parsing error or less than 1 or greater than 5, run while loop again.
Solution 2:[2]
Instead of declaring an int and using it as a number, you can declare a character and only use it as a number when it actually is a number, something like (pseudo-code):
char str_choice;
cin >> str_choice;
if ((str_choice <= '9') && (str_choice >= 0))
{
int choice = (int)str_choice - (int)'0'; // this part you might need to rework
switch (choice)
...
} else
cout << "You have not entered an integer, so you have lost the game" << endl;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cnp |
| Solution 2 | Dominique |
