Hello all. I'm currently working on a program where there is supposed to be 3 functions: 2 value returning functions and 1 void function. The first function I've created is supposed to generate a number from 0-2 to represent the computer's selection, and then assign each of those to be either R, S, or P. The second function is supposed to prompt the user for their answer, also R, S, or P, in addition to Q for Quit. I also have to add a validation loop to it so that it will keep prompting until it receives one of those answers. The void function I'm not worrying about yet, but it's supposed to be where the code goes for printing the results.
I'm stuck on the parts for the computer and human inputs now, even though I tried each one separately first. So my questions now are as follows:
1. How do I put in a validation loop within the userChoice function? I've tried a while and do-while loop so far, but they both keep prompting even when the user inputs R,S or P.
2. How can I fix my code so that the compChoice function will give me a more random answer instead of the same one repeatedly?
Any hints, suggestions, comments or thoughts would be greatly appreciated!
Here's my code so far (without the validation loops- they were within the userChoice function before; and char c/h are to be utilized in the void function):
Code:
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
char compChoice(int a)
{
char random;
random = rand () % 3;
if (random = 0)
random = 'R';
else if (random = 1)
random = 'S';
else
random = 'P';
return random;
}
char userChoice(string q, char a)
{
char answer;
cout << q;
cin >> answer;
answer = toupper(answer);
return answer;
}
void results(char c, char h)
{
}
int main()
{
// initialize the computer's random number generator
srand(time(0));
// declare variables
char R = 0;
char S = 0;
char P = 0;
char Q = 0;
char c = 0;
char h = 0;
// start loop
while (true)
{
// determine computer's choice
char comp = compChoice (S);
cout << endl << "Computer: "
<< comp << endl;
// prompt for, and read, the human's choice
char user = userChoice("Choose: [R,P,S,Q to Quit]: ", 'R');
// if human wants to quit, break out of loop
if (user = 'Q') break;
// print results
// end loop
}
// end program
return 0;
} // main