Mar 9, 2021 at 9:49pm UTC
#include <iostream>
using namespace std;
int main() {
int nextChoice;
cin >> nextChoice;
switch (nextChoice) {
case '0' :
cout << "Rock" << endl;
break;
case '1' :
cout << "Paper" << endl;
break;
case '2' :
cout << "Scissors" << endl;
break;
default :
cout << "Unknown" << endl;
break;
}
return 0;
}
its suppose to give me an answer depending on the number I insert it seems very basic and I'm not sure why its not working the only numbers allowed to be inputed are 1, 2 ,3.
Mar 9, 2021 at 9:51pm UTC
Just write
0
not
'0'
etc. in your case statements. You are comparing with ints, not chars.
Mar 10, 2021 at 4:34am UTC
Or char nextChoice; and leave all your cases as character constants.
Type 48 into your existing program :)
Mar 10, 2021 at 10:47am UTC
Consider:
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
#include <iostream>
using namespace std;
int main() {
char nextChoice {};
cin >> nextChoice;
switch (nextChoice) {
case '0' :
cout << "Rock\n" ;
break ;
case '1' :
cout << "Paper\n" ;
break ;
case '2' :
cout << "Scissors\n" ;
break ;
default :
cout << "Unknown\n" ;
break ;
}
}
or:
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
#include <iostream>
using namespace std;
int main() {
int nextChoice {};
cin >> nextChoice;
switch (nextChoice) {
case 0:
cout << "Rock\n" ;
break ;
case 1:
cout << "Paper\n" ;
break ;
case 2:
cout << "Scissors\n" ;
break ;
default :
cout << "Unknown\n" ;
break ;
}
}
Note to use \n instead of endl to output a new line.
There is a difference between type int and type char. 0 means an int of value 0. '0' means a char that has an ASCII value of 48.
Last edited on Mar 10, 2021 at 10:48am UTC