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();
}
}
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.