简体   繁体   中英

How do I make parts of a C++ program loop?

I've made a Rock, paper, scissors C++ program, but I'm having a hard time trying to get it to loop. In the program, it counts wins and losses (ties don't count) and when it ties, it continues until a win/loss. When a win/loss occurs, either variables (pWins for Player Wins or pLosses for Player Losses) is incremented. It asks if the user wants to play again, and if the user says no, the program ends and shows a score.

My problem is how to get the program to loop in cases of ties and the user choosing to continue the game. I know I should put some do-whiles, but i'm not sure how to write them.

if( pInput == ROCK && cInput == ROCK )
{
    std::cout << "You tied with me!" << std::endl;
}
else if( pInput == ROCK && cInput == SCISSORS )
{
    std::cout << "Drats! You win!" << std::endl;
}
else if( pInput == ROCK && cInput == PAPER )
{
    std::cout << "Hah! Here comes the Hug of Death!" << std::endl;
}
else if( pInput == SCISSORS && cInput == SCISSORS )
{
    std::cout << "Looks like we tied!" << std::endl;
}
else if( pInput == SCISSORS && cInput == ROCK )
{
    std::cout << "Hah! I smashed you, so I win!" << std::endl;
}
else if( pInput == SCISSORS && cInput == PAPER )
{
    std::cout << "Drats! You win!" << std::endl;
}
else if( pInput == PAPER && cInput == PAPER )
{
    std::cout << "Drats! We tied!" << std::endl;
}
else if( pInput == PAPER && cInput == SCISSORS )
{
    std::cout << "Hah! I win, because I'm a scissor!" << std::endl;
}
else if( pInput == PAPER && cInput == ROCK )
{
    std::cout << "Drats! You gave me the Hug of Death!" << std::endl;
}

std::cout << "You won " << pWins << " times, and lost " << pLosses << "times.";

}

FYI, you can use while , for and do-while to iterate in a loop.

You will have to determine the "exit" criteria for the loop.

Here's an example:

bool can_continue = true;
while (can_continue)
{
  cout << "Do you want to continue?";
  std::string answer;
  getline(cin, answer);
  if (answer = "no")
  {
    can_continue = false;
  }
}

Edit 1:
You can simplify your program by checking for ties:

if (pInput == cInput)
{
    std::cout << "You tied with me!" << std::endl;
}
else
{
  // Check for winning combinations.
}

You are right in thinking of using a do while loop. You could try something like this:

do {
    //your program
} while(condition);

This way your program would run once and then continue running if your condition is still true. The condition in your case would be some form of a game over boolean.

This could be the general structure:

int main() {
    do {
        pInput = getPlayerInput();
        cInput = getComputerInput();
        result = checkResult(pInput, cInput); // here you could put your code, and you 
                                              // could also set pWins and pLosses here

        if (result == tie) {
            playAgain = true;
        } else {
            playAgain = askPlayAgain();
        }
    } while (playAgain);

    print_results();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM