Hello. I'm pretty stuck on my program and I need help. I'm having trouble mainly with the void function I'm required to put in it. I'll place the code here. Also, it is not done yet. I just need it to compile before I can go to the next step.
Thanks I'll try that. :D I figured out I accidentally skipped some steps in the program so I took some out and went back, but now I'm stuck again. Keep in mind it's still quite unfinished.
#include <iostream>
usingnamespace std;
#include <cctype>
#include <cstdlib>
#include <ctime>
//Subprograms.
char getUser()
{
char user2 = 'X'; //Default value.
while(true)
{
cout << "Rock, paper, scissors? [R,P,S,Q to exit]" << endl;
cin >> user2;
cin.ignore(1000,10);
user2 = toupper(user2);
if (user2 == 'Q')
break;
elseif(user2 == 'R')
cout << "You chose rock." << endl;
elseif(user2 == 'S')
cout << "You chose scissors." << endl;
elseif(user2 == 'P')
cout << "You chose paper." << endl;
else
cout << "Invalid entry." << endl;
return user2;
}
}
char getUnit()
{
char unit2;
unit2 == 'R';
if(unit2 == 'R')
cout << "The computer chose rock." << endl;
elseif(unit2 == 'S')
cout << "The computer chose scissors." << endl;
elseif(unit2 == 'P')
cout << "The computer chose paper." << endl;
return unit2;
}
void winner()
{
}
int main()
{
// initialize the computer's random number generator
srand(time(0)); rand();
// declare variables
char user;
char unit;
// start loop
while(true)
{
// determine computer's choice
char unit = getUnit();
// prompt for, and read, the human's choice
char user = getUser();
// if human wants to quit, break out of loop
if (user == 'Q') break;
// print results
winner ();
}
// end loop
// end program
}
My new problem is I'm stuck in an infinite loop on the user's input. It outputs the user's input and then asks again and skips the rest of the program including the computer's output.
It returns Q and in the int main it quits the program. I tried adding more breaks, but the compiler gives me an error when I do saying illegal else without matching if, so I changed the else if to if statements, but then the else says the same error.
Update; Got it to work! I switched the loop that was giving me trouble into a switch statement.
First program is it won't compile cause I don't have user or unit in the void and I don't know how to write the parameters to be in void. Sorry. Also, when it was compiling before it wouldn't run and would exit the program right away or there'd just be a blank spot that I couldn't press any key on.
Edit; It compiles but won't run.. Thank you very much for your help.
In addition to what Moschops and IWishIKnew mentioned, you have a few other problems:
L20: The break is going to exit out of the while loop, but there is no return statement after the while loop. Most compilers will generate a return 0. Not what you want.
L22: What if the user didn't enter R,P or S? You're going to return that.
L30: What's the point of the while loop here? You're never going to exit this loop.
L99: You allow the user input to be upper or lower case in your switch statement, but in winner(), you're only checking for upper case.
Line 86,87: Unneeded. You've declared user and unit at 92 and 94.
Line 49: You need to pass unit and used into your winner function. void winner (char unit, char user)
Line 51,60,69: There's an easier way to check for a tie. if (user == unit)
Linr 19 & 96: You allow the user to enter 'Q' or 'q', but only check 'Q' to exit.