trouble with rock paper scissor problem

hi all,
I am a beginner, I am trying to do rock paper scissor problem, but even after numerous attempts my program does n't work, I'll appreciate any help,
below is my code. thanks again.
......................
/*
* rock.cpp
*
* Created on: Jan 17, 2014
* Author: BK
*/
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>

using namespace std;

string compchoice();
string userchoice();

int main() {

//cout<<"computer picks a choice here"<<compchoice()<<endl;
//cout<<"pick your choice "<<userchoice()<<endl;
string comp, user;
comp = compchoice();
user = userchoice();

while (userchoice() != compchoice()) {
if (userchoice() != compchoice())
{
cout << " go again \n\t";
comp = compchoice();
user = userchoice();

}
else {
cout << "congratulations\t computer choice was "<<comp<<" user choice is "<<user;
//comp = compchoice();
//user = userchoice();
}
}

}

string compchoice() {

srand(time(0));

int ch;

string choiceStr;

ch = rand() % 3 + 1;

switch (ch) {
case 1:
choiceStr = "rock";
break;
case 2:
choiceStr = "paper";
break;
case 3:
choiceStr = "scissors";
break;
default:
cout << "computer menu.... existing";
//break;
}
return choiceStr;

}

string userchoice() {

string choiceStr;

int choice;

cout << " 1. rock \n";

cout << " 2. paper \n";

cout << " 3. scissors \n";

cout << " user menu make selection : \n";

cin >> choice;

cout << endl;

switch (choice) {
case 1:
choiceStr = "rock";
break;
case 2:
choiceStr = "paper";
break;
case 3:
choiceStr = "scissors";
break;
default:
choiceStr = "enter only 1, 2, or 3";
//break;
}

return choiceStr;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() 
{
    string comp = compchoice();
    string user = userchoice();

    while (userchoice() != compchoice()) 
    {
        if (userchoice() != compchoice())
       {
            cout << " go again \n\t";
            comp = compchoice();
            user = userchoice();
        }
        else 
        {
            cout << "congratulations\t computer choice was "<<comp<<" user choice is "<<user;
        }
    }
}


Every time you call userchoice or compchoice, you either ask the user for a new choice, or you generate a new random choice for the computer.

So, after lines 3 and 4 above, on line 6 (and 8) you want to compare the choices already obtained, not get new choices and compare those.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() 
{
    string comp = compchoice();
    string user = userchoice();

    while (comp != user) 
    {
        cout << " go again \n\t" ;
        comp = compchoice() ;
        user = userchoice() ;
    }

    cout << "congratulations\t computer choice was "<<comp<<" user choice is "<<user;
}



Hi Cire, thanks for help.
The program runs fine now but the random number generator seem to have stuck only at one choice (in my case it is first choice 'rock').
I am calling srand(time(0) as well, to my understanding it should generate new random number every time, but i get the same choice over and over again.
Hi Cire, Thanks for the help, I got it figured out, the problem was not with the rand function, but with the while loop, I was calling the compchoice() again which was changing the computer choice everytime,
simply removing it from the while loop executed the program well and fine,
thank you again Cire.
below is my corrected code.

while (comp != user)
{
cout << " go again \n\t" ;

user = userchoice() ;
}
Topic archived. No new replies allowed.