Your code really needs to be indented for readability purposes.
Anyways, global identifiers are typically considered bad style. They still work, but if you can come up with a solution without globals, then you should do that. And since you return the user's choice anyways, why do you need to keep it in a global variable?
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
> `srandom' undeclared (first use this function)
Depending on what compiler you are using that srandom function may not be recognised.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Well the problem is your comparison. You cannot use == to compare chars. Wait you're using a single char... oops.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Maybe you need to read a tutorial about functions and the scope of variables within a function? Because you have more or less got it. You just have to rename a few of your variables.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char getUserChoice ( void );
int main()
{
char again;
int computer = 0;
int player = 0;
int tie = 0;
do
{
char comp;
srand ( time ( 0 ) );
int n = rand() % 3;
n++;
if ( n == 1 )
{
comp = 'R';
}
else if ( n == 2 )
{
comp = 'P';
}
else
{
comp = 'S';
}
char summat = getUserChoice();
if ( comp == summat )
{
cout << "It's a tie.\n";
tie += 1;
}
else if ( ( comp == 'R' ) && ( summat == 'S' ) )
{
cout << "Rock smashes scissors. You Lose.\n";
computer += 1;
}
else if ( ( comp == 'P' ) && ( summat == 'R' ) )
{
cout << "Paper covors rock. You Lose.\n";
computer += 1;
}
else if ( ( comp == 'S' ) && ( summat == 'P' ) )
{
cout << "Scissors cut paper. You Lose.\n";
computer += 1;
}
else if ( ( comp == 'R' ) && ( summat == 'P' ) )
{
cout << "Paper covors rock. You Win.\n";
player += 1;
}
else if ( ( comp == 'P' ) && ( summat == 'S' ) )
{
cout << "Scissors cut paper.You Win.\n";
player += 1;
}
else if ( ( comp == 'S' ) && ( summat == '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 ( void )
{
char choice;
cout << "Please enter Rock, Paper, or Scissors: ";
cin >> choice;
cin.ignore ( 1000, '\n' );
choice = toupper ( choice );
return ( choice );
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>But what needs to be displayed is the users choice.
Have a go what do you think?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
can you post the entire code please.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
hmm..
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
char getUserChoice();
char getCompChoice();
int main()
{
char userChoice;
char compChoice;
userChoice = getUserChoice();
compChoice = getCompChoice();
cout << "User picked: " << userChoice << endl;
cout << "Computer picked: " << compChoice << endl;
cin.get();
return ( 0 );
}
char getUserChoice ()
{
int again = 0;
do
{
char x;
cout << "Please enter Rock, Paper, or Scissors: ";
cin >> x;
cin.ignore ( 1000, '\n' );
x = toupper ( x );
if ( x == 'R' )
{
return ( 'R' );
}
else if ( x == 'P' )
{
return ( 'P' );
}
else if ( x == 'S' )
{
return ( 'S' );
}
else
{
again = 1;
cout << "Not a good choice. ";
}
}
while ( again == 1 );
}
char getCompChoice()
{
srand ( time ( 0 ) );
int n = rand() % 3;
n++;
if ( n == 1 )
{
return ( 'R' );
}
else if ( n == 2 )
{
return ( 'P' );
}
else
{
return ( 'S' );
}
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>Same error came up.
Ok this may be a compiler issue, cos it worked here. I'm not entirely sure what it could be although my hunch is this line (and variants thereof) might be the issue:-
if ( x == 'R' )
{
return ( 'R' );
}
Then again I could be wrong?
char getUserChoice(void); <- you might need a void there also?
>I am understanding better, your not just telling me the answer, that is what I was looking for in a forum. so Thanks.
Yes, tis true, but it also happens to be very time consuming. He he.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
What compiler are you using?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439