【问题标题】:C++ Won't exit do while loopC ++不会退出do while循环
【发布时间】:2013-09-26 04:27:14
【问题描述】:

我刚刚开始学习 C++。我目前正在使用 Bloodshed Dev C++。 d我正在创建一个非常基本和简单的石头剪刀布游戏。除了退出循环外,程序中的所有内容都正常工作。这是我的代码:

    /* 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();只是为了防止程序在完成运行后立即退出。

如果我把其他所有东西都搞砸了,只留下 do while 和影响它的代码,这就是我所拥有的。

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

} while (keep_going = 'y');

如果我专门输入字母“y”,它应该只继续并再次启动循环,但无论我输入什么,它似乎都无法正常工作。提前感谢您提供的所有帮助。

【问题讨论】:

    标签: c++ loops infinite-loop do-while


    【解决方案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";
    }
    

    【讨论】:

    • 啊,哈哈,谢谢,我笨。只是那些简单的语法错误之一。非常感谢!我以后一定会寻找那种东西的!
    • 这发生在我们最好的人身上。我记得在大学的时候,我的教授会建议我们改变比较的顺序,比如42 == foo,这样我们就不会遇到同样的错误。它有效,但对于说英语的人来说,它并不是最直观的。你的来电。 :)
    • 哈哈,明白了,再次感谢。我真的很感激:)
    【解决方案2】:

    比较事物时使用== 而不是== 使左值等于右值,而== 用于比较两个对象。

    正确代码:

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

    【讨论】:

      【解决方案3】:

      除了使用==,您还应该使用cin.get(),这样用户就不需要按Enter:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多