Hi everybody, i cant seem to make my program to pass the first user input. I know i have to put it inside a do while loop, but everyone of my attempts results in an infinite loop. I'm also aware that i should use a bool to make it work, but i've spent a couple of hours trying to figure it out and still cant =(
If anybody could help me out, i would really appreciate it. Thanks guys!

//Will Rodriguez 
#include <cstdlib>
#include <iostream>
#include <ctime>


using namespace std;    
// Function
void Winner(int choice1, int choice2); 
void menu();

int main(int argc, char *argv[])
{
    
    cout << "Welcome to the Greatest Game of all Times:\n"                      //Introduction to the program
         << "\tRock Papers & Scissors!\n\n"
         << " You will play against the computer\n"
         << " you gain one point for every win\n"
         << " the game goes to 10, so try your best!\n"
         << "------------------------------------------\n\n";
   
          
   menu();                                                                      //Show Menu
   char playerChoice;
   cout<<"Enter your choice: ";
   cin>>playerChoice;
          
                                                                                 
                     switch(playerChoice)
                     {
                         case 'r': case 'R':
                              cout<<"Player: Rock"<<endl;
                              playerChoice = 0;
                              break;
                         case 'p': case 'P':
                              cout<<"Player: Paper"<<endl;
                              playerChoice = 1;
                              break;
                         case 's': case 'S':
                              cout<<"Player: Scissors"<<endl;
                              playerChoice = 2;
                              break;
                         case 'q': case 'Q':
                              exit(0);
                         default:
                              cout<<"Play option does not exist!"<<endl;
                     }
    srand((unsigned)time(0));
    int compChoice = rand()%3;

                     switch(compChoice)
                     {
                         case 0:
                              cout<<"Computer: Rock"<<endl;
                              break;
                         case 1:
                              cout<<"Computer: Paper"<<endl;
                              break;
                         case 2:
                              cout<<"Computer: Scissors"<<endl;
                              break;
                         default:
                              cout<<"Program is not functioning properly."<<endl;  
                     }
                     
                     Winner(playerChoice, compChoice);                      
    system("PAUSE");
    return 0;
}

    void menu()                                                                 //Menu
    {
              cout <<"(R)ock\n";
              cout <<"(P)aper\n";
              cout <<"(S)cissors\n";
              cout <<"----------\n";
    }
    
    void Winner(int choice1, int choice2)
    {
    int playerWin = 0;
    int compWin = 0;
    
    if(choice1 == 0 && choice2 == 0 || choice1 == 1 && choice2 == 1 || choice1 == 2 && choice2 == 2)
    {
              cout <<"I'ts a tie!\n";
    }
    else if(choice1 == 0 && choice2 == 1)
    {
              cout <<"Paper covers rock!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 1 && choice2 == 0)
    {
              cout <<"Paper covers rock!\n"
                   <<"You win!\n";
              playerWin++;
    }
    else if(choice1 == 0 && choice2 == 2)
    {
              cout <<"Rock crushes scissors!\n"
                   <<"You win!\n";
              playerWin++;
    }
    else if(choice1 == 2 && choice2 == 0)
    {
              cout <<"Rock crushes scissors!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 1 && choice2 == 2)
    {
              cout <<"Scissors cut paper!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 2 && choice2 == 1)
    {
              cout <<"Scissors cut paper!\n"
                   <<"You win!\n";
              playerWin++;
    }
    
    cout <<"\n"
         <<"Score is\nPlayer:" <<playerWin <<endl
         <<"Computer:"        <<compWin   <<endl;     
    }

Just use an infinite loop. You already have the exit statement in there for when they quit, so you don't need a condition for the while statement.

while(true)
{
    // stick everything that needs to repeated here
}

Just use an infinite loop. You already have the exit statement in there for when they quit, so you don't need a condition for the while statement.

while(true)
{
    // stick everything that needs to repeated here
}

I tried that and the program repeats itself infinite times. >.< what can i do about that?

>> I tried that and the program repeats itself infinite times. >.< what can i do about that?

Hard to say without seeing what you tried. If you get into here:

switch(playerChoice)
                     {
                         case 'r': case 'R':
                              cout<<"Player: Rock"<<endl;
                              playerChoice = 0;
                              break;
                         case 'p': case 'P':
                              cout<<"Player: Paper"<<endl;
                              playerChoice = 1;
                              break;
                         case 's': case 'S':
                              cout<<"Player: Scissors"<<endl;
                              playerChoice = 2;
                              break;
                         case 'q': case 'Q':
                              exit(0);
                         default:
                              cout<<"Play option does not exist!"<<endl;
                     }

you should exit, so I can only imagine that the while loop is in the wrong spot. Post the attempt with the while loop.

Hello hous3aholik,
This is where you got wrong.You declared playerchoice as a character variable and you are assigning integer 0 to it in the switch cases.

char playerChoice;
playerChoice = 0;//<--

So you cannot compare it with integer values in the winner() function and the parameters in the winner function are also integers.

Edited 5 Years Ago by Arbus: n/a

all right, what im trying to do is to repeat the program until the user gets a score of 10 points, but it only works with the first attempt. so this is what i did

//Will Rodriguez 
#include <cstdlib>
#include <iostream>
#include <ctime>


using namespace std;    
// Function
void Winner(int choice1, int choice2); 
void menu();

int main(int argc, char *argv[])
{
    
    cout << "Welcome to the Greatest Game of all Times:\n"                      //Introduction to the program
         << "\tRock Papers & Scissors!\n\n"
         << " You will play against the computer\n"
         << " you gain one point for every win\n"
         << " the game goes to 10, so try your best!\n"
         << "------------------------------------------\n\n";
   
          
   
   char playerChoice;
   cout<<"Enter your choice: ";
   cin>>playerChoice;
                     while(true)
                     {                                                                                               
                     menu();
                     switch(playerChoice)
                     {
                         case 'r': case 'R':
                              cout<<"Player: Rock"<<endl;
                              playerChoice = 0;
                              break;
                         case 'p': case 'P':
                              cout<<"Player: Paper"<<endl;
                              playerChoice = 1;
                              break;
                         case 's': case 'S':
                              cout<<"Player: Scissors"<<endl;
                              playerChoice = 2;
                              break;
                         case 'q': case 'Q':
                              exit(0);
                         default:
                              cout<<"Play option does not exist!"<<endl;
                     }
                     
    srand((unsigned)time(0));
    int compChoice = rand()%3;

                     switch(compChoice)
                     {
                         case 0:
                              cout<<"Computer: Rock"<<endl;
                              break;
                         case 1:
                              cout<<"Computer: Paper"<<endl;
                              break;
                         case 2:
                              cout<<"Computer: Scissors"<<endl;
                              break;
                         default:
                              cout<<"Program is not functioning properly."<<endl;  
                     }
                     Winner(playerChoice, compChoice);
                     }                         
    system("PAUSE");
    return 0;
}

    void menu()                                                                 //Menu
    {
              cout <<"(R)ock\n";
              cout <<"(P)aper\n";
              cout <<"(S)cissors\n";
              cout <<"(Q)uit\n";
              cout <<"----------\n";
              
    }
    
    void Winner(int choice1, int choice2)
    {
    int playerWin = 0;
    int compWin = 0;
    
    if(choice1 == 0 && choice2 == 0 || choice1 == 1 && choice2 == 1 || choice1 == 2 && choice2 == 2)
    {
              cout <<"I'ts a tie!\n";
    }
    else if(choice1 == 0 && choice2 == 1)
    {
              cout <<"Paper covers rock!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 1 && choice2 == 0)
    {
              cout <<"Paper covers rock!\n"
                   <<"You win!\n";
              playerWin++;
    }
    else if(choice1 == 0 && choice2 == 2)
    {
              cout <<"Rock crushes scissors!\n"
                   <<"You win!\n";
              playerWin++;
    }
    else if(choice1 == 2 && choice2 == 0)
    {
              cout <<"Rock crushes scissors!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 1 && choice2 == 2)
    {
              cout <<"Scissors cut paper!\n"
                   <<"You lose!\n";
              compWin++;
    }
    else if(choice1 == 2 && choice2 == 1)
    {
              cout <<"Scissors cut paper!\n"
                   <<"You win!\n";
              playerWin++;
    }
    
    cout <<"\n"
         <<"Score is\nPlayer:" <<playerWin <<endl
         <<"Computer:"        <<compWin   <<endl;     
    }

I need to be able to enter a new guess every game, right? Right now I'm only asked once. Adjust the while loop position.

Hello hous3aholik,
This is where you got wrong.You declared playerchoice as a character variable and you are assigning integer 0 to it in the switch cases.
char playerChoice;
playerChoice = 0;
So you cannot compare it with integer values in the winner() function and the parameters in the winner function are also integers.

So i should try to change the value of playerChoice to an int? and also change the value of Zero?

Edited 5 Years Ago by hous3aholik: n/a

I need to be able to enter a new guess every game, right? Right now I'm only asked once. Adjust the while loop position.

adjust the while loop into where? where should i put it in order for the program to work? once again guys im really sorry if i sound like i have no idea what im talking about, i just have little experience with C++

Change playerchoice to int. Ask the user to enter 0 for rock ,1 for paper and 2 for scissors
or
have a new integer variable to pass it in the winner function.

Edited 5 Years Ago by Arbus: n/a

Hello hous3aholik,
This is where you got wrong.You declared playerchoice as a character variable and you are assigning integer 0 to it in the switch cases.

char playerChoice;
playerChoice = 0;//<--

So you cannot compare it with integer values in the winner() function and the parameters in the winner function are also integers.

It's not good programming style and it's a good thing to point out as the OP may not have noticed, but it'll actually work here. 0, 1, and 2 are valid char values and will be converted to integers with values 0, 1 or 2 when the function is called, so in this particular case it'll work, though it may be a bit confusing to read the code.

The problem is the while loop position (though there are some other things too).

>> adjust the while loop into where?

cout<<"Enter your choice: ";
   cin>>playerChoice;
                     while(true)
                     {                                                                                               
                     menu();

Put the code in red AFTER the code in green rather than before.

It's not good programming style and it's a good thing to point out as the OP may not have noticed, but it'll actually work here. 0, 1, and 2 are valid char values and will be converted to integers with values 0, 1 or 2 when the function is called, so in this particular case it'll work, though it may be a bit confusing to read the code.

The problem is the while loop position (though there are some other things too).

But when i changed playerchoice to int and also cases like case 'r' etc to case 0 it worked finely on my system (I made this change in the code written in the thread which is not having while loop).

Edited 5 Years Ago by Arbus: n/a

>> adjust the while loop into where?

cout<<"Enter your choice: ";
   cin>>playerChoice;
                     while(true)
                     {                                                                                               
                     menu();

Put the code in red AFTER the code in green rather than before.

I did what you did, and it works!!!! :D but now for some reason it wont record the results. the program has to go up to ten.
once again thank you so much for your help

But when i changed playerchoice to int and also cases like case 'r' etc to case 0 it worked finely on my system.

Yes, that works. Your solution is not incorrect. However, it will also work without making your changes and keeping the switch statement as it is and keeping playerChoice as a char. The main problem the OP had was that once you selected rock, paper, or scissors, you were stuck there forever because the cin statement was BEFORE the while loop, not INSIDE the while loop.

Edited 5 Years Ago by VernonDozier: n/a

Yes, that works. Your solution is not incorrect. However, it will also work without making your changes and keeping the switch statement as it is and keeping playerChoice as a char. The main problem the OP was that once you selected rock, paper, or scissors, you were stuck there forever because the cin statement was BEFORE the while loop, not INSIDE the while loop.

Yeah thanks to you i was able to see that, but now every single time i input a new value, the count of the attempts seem to restart. it does not keep the score until ten. do you have any idea on how to fix this?

I did what you did, and it works!!!! :D but now for some reason it wont record the results. the program has to go up to ten.
once again thank you so much for your help

You are welcome. The reason the results aren't working is because you are declaring the playerWin and compWin variables inside of a function. Make them global variables by declaring them BEFORE the main function.

There are other solutions as well, but I am assuming you have not covered passing by reference versus passing by value and therefore for now I suggest making the variables global and that problem will be solved (unless there is another bug in there too).

You are welcome. The reason the results aren't working is because you are declaring the playerWin and compWin variables inside of a function. Make them global variables by declaring them BEFORE the main function.

There are other solutions as well, but I am assuming you have not covered passing by reference versus passing by value and therefore for now I suggest making the variables global and that problem will be solved (unless there is another bug in there too).

Yeah thats using the ampersand (&) but i have little understanding of it since i missed the class where they taught that =( but once again, thank you very much.

You are welcome. The reason the results aren't working is because you are declaring the playerWin and compWin variables inside of a function. Make them global variables by declaring them BEFORE the main function.

There are other solutions as well, but I am assuming you have not covered passing by reference versus passing by value and therefore for now I suggest making the variables global and that problem will be solved (unless there is another bug in there too).

Vernon im sorry to bother you again, i fixed the count, so how do i stop the program whenever one of the scores reaches 10 points?

You'll need to change the while condition from:

while(true)

to

while(/* some condition */)

If the game is supposed to stop after either the human or the computer win ten times, you'll want to figure out a comparison to make that tests to see whether that has occurred and make that the while loop's condition.

This question has already been answered. Start a new discussion instead.