Reputation: 3
The validatePlay function I've written is supposed to accept an entered character that matches a play type.
char validatePlay();
void getHandPlayed(char);
int main()
{
char play= validatePlay();
validatePlay();
getHandPlayed(play);
system("pause");
}
char validatePlay()
{
char play;
cout << "enter play (R)ock, (P)aper or (S)cissor ";
cin >> play;
while (cin.fail() || play != 'R' && play != 'P' && play != 'S')
{
cin.clear();
cin.ignore(80, '\n');
cout << "re-enter play (R)ock, (P)aper or (S)cissor ";
cin >> play;
}
return play;
}
void getHandPlayed(char play)
{
switch(play)
{
case 'R':
cout << "Rock"<<endl;
break;
case 'P':
cout << "Paper"<<endl;
break;
case 'S':
cout << "Scissors"<<endl;
break;
}
}
It sort of works, but for some reason, the cout and cin appear twice no matter what I enter but it's only taking in the first char I enter. It ends up looking like this.
enter play (R)ock, (P)aper or (S)cissor S
enter play (R)ock, (P)aper or (S)cissor R
Scissors
Press any key to continue . . .
I can't figure out what I'm doing wrong here.
Upvotes: 0
Views: 200
Reputation: 244
You call validatePlay() twice.
The first time is here:
char play= validatePlay();
The second time is here:
validatePlay();
You are only storing and using the result from the first call.
Upvotes: 2
Reputation: 72044
You have this in your main:
char play= validatePlay();
validatePlay();
What do you think that does?
Upvotes: 0