1

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」を入力した場合にのみ続行してループを再開する必要がありますが、何を入力しても正しく機能しないようです。あらゆる助けを前もって感謝します。

4

4 に答える 4

3

(割り当て)==の代わりに (比較)を使用する必要があります。=

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";
}
于 2013-09-26T04:29:59.293 に答える
3

物事を比較するとき==の代わりに使用します。左の値を右の値に等しくしますが、2 つのオブジェクトを比較するために使用されます。====

正しいコード:

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

} while (keep_going == 'y');
于 2013-09-26T04:35:02.777 に答える
1

の使用に加えて、ユーザーが Enter キーを押す必要がないように==使用する必要があります。cin.get()

char keep_going;
do {     
    cout << "\n\nKeep playing?\n";
    keep_going = cin.get();
} while (keep_going == 'y');
于 2013-09-26T04:33:36.183 に答える
-1

//探している構文は次のとおりです。

char keep_going;
do {
//some statements
} while(cin.get(keep_going));
于 2015-01-17T14:35:52.243 に答える