Reputation: 1
#include <iostream>
using namespace std;
int main() {
cout << "1. Facebook" << endl;
cout << "2. Twitter" << endl;
cout << "3. Instagram" << endl;
cout << "4. SnapChat" << endl;
int input;
do {
cout << "Enter a selection > ";
cin >> input;
switch(input) {
case 1:
cout << "Facebook is Loading..." << endl;
break;
case 2:
cout << "Twitter is Loading..." << endl;
case 3:
cout << "Instagram is Loading..." << endl;
case 4:
cout << "SnapChat is Loading..." << endl;
break;
default:
cout << "Wrong Selection" << endl;
}
}while(input =! 1 && input =! 2 && input != 3 && input != 4);
return 0;
}
I want to retry the progress if I got the wrong selection and I got confused and dunno what to do, so can someone tell me what I should do so I can make it?
Upvotes: 0
Views: 46
Reputation: 106226
Firstly...
input =! 1 && input =! 2 && input != 3 && input != 4
... those =!
should be !=
.
Secondly, if someone types non-numeric input, cin >> input
will fail and all future input attempts will instantly fail - without even waiting for the user to type anything further. You need to clear the error state and ignore any bad input characters remaining in the stream - say up to the end of the line:
default:
cout << "Wrong Selection" << endl;
std::cin.clear(); // clear error state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');.
You'll need to #include <limits>
atop your program to use std::numeric_limts<>
.
Separately, you're first two case
statements lack a break
and will fall through to execute the following case
(s) code....
I'd recommend explicitly checking cin >> input
for success too, though with C++11 it guarantees to set input
to 0
on failure so it will work reliably. With C++03 there's no such guarantee and you may get arbitrary garbage left in input
after a failure, which might coincidentally match one of the case
s. To handle this explicitly:
if (!(cin >> input)) input = -1; // sentinel meaning erroneous
Upvotes: 1
Reputation: 4023
Add a break
statement after each case
:
case 2:
cout << "Twitter is Loading..." << endl;
break;
case 3:
cout << "Instagram is Loading..." << endl;
break;
Change =!
to !=
while(input != 1 && input != 2 && input != 3 && input != 4);
Upvotes: 0
Reputation: 167
You need to make sure you add your breaks after each case if you want the switch statement to break/exit. Else everything underneath is going to be executed. Also you mixed up the != (does not equal) and wrote =!
#include <iostream>
using namespace std;
int main()
{
cout << "1. Facebook" << endl;
cout << "2. Twitter" << endl;
cout << "3. Instagram" << endl;
cout << "4. SnapChat" << endl;
int input;
do {
cout << "Enter a selection > ";
cin >> input;
switch(input) {
case 1:
cout << "Facebook is Loading..." << endl;
break;
case 2:
cout << "Twitter is Loading..." << endl;
break; // add break
case 3:
cout << "Instagram is Loading..." << endl;
break; // add break
case 4:
cout << "SnapChat is Loading..." << endl;
break; // add break
default:
cout << "Wrong Selection" << endl;
}
} while(input != 1 && input != 2 && input != 3 && input != 4); // != not =!
return 0;
}
Upvotes: 0