In C++, how to deal with other data types in switch cases?
Like cases are of int type and user inputs char type.
int play(Player &Player1, Player &Player2)
{
int answer = 0, guess = 0;
srand(22);
answer = rand() %100;
bool win = false;
while (!win)
{
cout<< "Player1s' turn to guess " <<end1;
guess = Player1.getGuess();
win = checkForWin(guess, answer);
if (win) return 0;
cout<< "Player2's turn to guess " <<end1;
guess = Player2.getGuess();
win = checkForWin(guess, answer);
}
}
int main()
{
system("COLOR 1E");
Humanplayer Player1,Player2;
play( Player1, Player2);
system("pause");
getch();
}
**left window**
:start
{
cout<<"Select Options \n";
cout<<" 1 for Human vs Human\n 2 for Human vs Computer\n 3 for Computer vs Computer
cin>>opt;
switch(opt)
{
case 1:
play( Player1, Player2);
break;
case 2:
play( Player1, Comp1);
break;
case 3:
play(Comp1, Comp2);
break;
default:
cout<<" Invalid entry!!!\n Please enter 1,2 or 3!!!\n";
goto start;
}
}
**right window**
Select Options
1 for Human vs Human
2 for Human vs Computer
3 for Computer vs Computer
h
If the user enters a char instead if an int I do not know how to handle this in the switch.