I am writing code for a homework assignment. I have to write a function, and in the function prompt the user to enter either rock, paper, or scissors. I did it successfully declaring a char (choice) globably, but I was told this wasnt a good idea, even though it worked. Why wouldn't this be a good idea?
actually here is my code so far...
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char getUserChoice(char);
char choice;
int main()
{
char again;
int computer = 0;
int player = 0;
int tie = 0;
do {
char comp;
srandom(time(0));
int n = random() % 3;
n++;
if (n == 1) {
comp = 'R';
} else if (n == 2) {
comp = 'P';
} else {
comp = 'S';
}
if (comp == getUserChoice(choice)) {
cout << "It's a tie.\n";
tie += 1;
} else if ((comp == 'R') && (choice == 'S')) {
cout << "Rock smashes scissors. You Lose.\n";
computer += 1;
} else if ((comp == 'P') && (choice == 'R')) {
cout << "Paper covors rock. You Lose.\n";
computer += 1;
} else if ((comp == 'S') && (choice == 'P')) {
cout << "Scissors cut paper. You Lose.\n";
computer += 1;
} else if ((comp == 'R') && (choice == 'P')) {
cout << "Paper covors rock. You Win.\n";
player += 1;
} else if ((comp == 'P') && (choice == 'S')) {
cout << "Scissors cut paper.You Win.\n";
player += 1;
} else if ((comp == 'S') && (choice == 'R')) {
cout << "Rock smashes scissors.You Win.\n";
player += 1;
}
cout << "Computer wins: " << computer << endl << "User wins: " << player << endl << "Ties: "
<< tie << endl;
cout << "Play again?(Yes or No): ";
cin >> again;
cin.ignore(1000, '\n');
again = toupper(again);
} while (again == 'Y')
;
return(0);
}
char getUserChoice(char)
{
cout << "Please enter Rock, Paper, or Scissors: ";
cin >> choice;
cin.ignore(1000, '\n');
choice = toupper(choice);
return(choice);
}
How would I return the users input using a function without returning a variable? char getUserChoice()