Здравствуйте, есть другой способ конвертировать int обратно в символ, см. Комментарий о половине кода.Я думаю об использовании переключателя или оператора if, но я не могу понять, как применить его.я использовал char RPS [] = {'?', 'R', 'P', 'S'};
#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>
//human choice function
int hchoice()
{
char entry;
int usrchoice = 0;
while (1)
{
cout <<"Choose R for Rock P for Paper, S for Sissors or Q for quit ";
cin >> entry;
cin.ignore(1000, 10);
switch (toupper(entry)){
case 'R':
usrchoice = 1;
break;
case 'P':
usrchoice = 2;
break;
case 'S':
usrchoice = 3;
break;
case 'Q':
usrchoice = -1;
break;
}
if (usrchoice != 0)break;
cout << "Invalid Entry" <<endl;
}
return usrchoice;
}
//Computer choice function
int compchoice()
{
return (1 + rand() % 3);
}
void printresults(int computer, int human)
{
//Number to char converter? Can i use a switch here?
char RPS[] = {'?', 'R', 'P', 'S'};
cout << "Computer:" << RPS[computer];
cout << ", Human:" << RPS[human];
cout << ", ";
if (computer == human){
cout <<"tie";
}
else if ( ( human==1 && computer == 2) || (human == 2 && computer == 3) || (human == 3 && computer == 1)){
cout << "computer wins!";
}
else {
cout <<"Human Wins!";
}
cout << endl;
}
int main()
{
// initialize the computer's random number generator
srand(time(0)); rand();
// declare variables
int human = 0, computer = 0;
// start loop
while (1)
{
// determine computer's choice
computer = compchoice();
// prompt for, and read, the human's choice
human = hchoice();
// if human wants to quit, break out of loop
if (human == -1) break;
// print results
printresults(computer, human);
cout << endl;
// end loop
}//while
// end program
return 0;
}