C ++将不会退出做while循环(C++ Won't exit do while loop

2019-10-18 16:31发布

我刚开始学习C ++。 我目前使用的流血开发的C ++。 dI'm创建一个非常基本的和简单的岩石剪刀布游戏。 在节目中的一切除了退出循环是否正常工作。 这里是我的代码:

    /* FILE INFO

File Name: Chapter 3 - Project 1.cpp
Author: Richard P.
P#: ---------
Assignment: Chapter 3 Project 1

*/

#include <iostream>
using namespace std;
int main()
{

char Player1_choice;
char Player2_choice;

char keep_going;

cout << "Welcome to Rock, Paper, Scissors! The rules of the game are simple:\n\n" 
     << "Enter either the letter P (for Paper), S (for Scissors), or R (for Rock)\n\n"
     << "Paper covers rock, Rock breaks scissors, Scissors cut paper\n\n"
     << "If both players pick the same choice then it is a draw!\n"
     << "-----------------------------------------------------------------------------\n\n";

do
{
     cout << "Okay, player 1, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
     cin >> Player1_choice;

     switch (Player1_choice) //I COULD DO A NESTED SWITCH STATMENT BUT FOR VARIETY I AM USING SWITCH AND IF STATMENTS.
     {
            case 'R':
            case 'r':

                 cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
                 cin >> Player2_choice;

                 if (Player2_choice == 'R' || Player2_choice == 'r')
                      cout << "It's a draw!\n";
                 else if (Player2_choice == 'P' || Player2_choice == 'p')
                      cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
                 else if (Player2_choice == 'S' || Player2_choice == 's')
                      cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
                 else
                      cout << "That is not a valid entry! Please read the rules and play again :)\n";


                 break;

            case 'P':
            case 'p':

                 cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
                 cin >> Player2_choice;

                 if (Player2_choice == 'R' || Player2_choice == 'r')
                      cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
                 else if (Player2_choice == 'P' || Player2_choice == 'p')
                      cout << "It's a draw!\n";
                 else if (Player2_choice == 'S' || Player2_choice == 's')
                      cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
                 else
                      cout << "That is not a valid entry! Please read the rules and play again :)\n";

                 break;  

            case 'S':
            case 's':

                 cout << "\n\nOkay, player 2, what is your choice? Is it R(rock), P(paper), or S(scissors)?\n";
                 cin >> Player2_choice;

                 if (Player2_choice == 'R' || Player2_choice == 'r')
                      cout << "Sorry Player 1, you lose!\n\n THE WINNER IS PLAYER 2";
                 else if (Player2_choice == 'P' || Player2_choice == 'p')
                      cout << "Sorry Player 2, you lose!\n\n THE WINNER IS PLAYER 1";
                 else if (Player2_choice == 'S' || Player2_choice == 's')
                      cout << "It's a draw!\n";
                 else
                      cout << "That is not a valid entry! Please read the rules and play again :)\n";

                 break;  

            default:
                    cout << "That is not a possible entry.\n";        
     }

     cout << "\n\nKeep playing?\n";
     cin >> keep_going;

} while (keep_going = 'y');

     cout << "You have chosen not to keep playing. Press Enter to exit the game";
cin.get();
cin.get();
return 0;
}

所述cin.get(); 简直是有从一旦完成运行immedietly退出保持程序。

如果我还有胸围下来的一切,只一会儿,影响代码的话,这里就是我离开做的。

    char keep_going;
do
{     
         cout << "\n\nKeep playing?\n";
         cin >> keep_going;

} while (keep_going = 'y');

它应该只继续和重新开始循环,如果我专门输入字母“Y”,但不管我选择它只是似乎没有正常工作。 预先感谢任何及所有的帮助。

Answer 1:

您应该使用== (比较),而不是= (转让):

do
{     
         cout << "\n\nKeep playing?\n";
         cin >> keep_going;

} while (keep_going == 'y');

原因是,是,当你分配一个变量,计算结果为价值true被返回。 例如:

if(foo = 42) { //equivalent to if(true) {...}
    cout << "This is evaluating variable assignment";
} else {
    cout << "This line will never be reached";
}


Answer 2:

使用==而不是=比较东西的时候。 =使左值等于右边,而==用来比较两个对象。

正确的代码:

do
{     
     cout << "\n\nKeep playing?\n";
     cin >> keep_going;

} while (keep_going == 'y');


Answer 3:

除了使用== ,你应该使用cin.get()所以用户不需要按Enter键:

char keep_going;
do {     
    cout << "\n\nKeep playing?\n";
    keep_going = cin.get();
} while (keep_going == 'y');


Answer 4:

//你要找的语法是这样的:

char keep_going;
do {
//some statements
} while(cin.get(keep_going));


文章来源: C++ Won't exit do while loop