我剛開始學習C++。我目前使用Bloodshed Dev C++。我是在創建一個非常簡單和簡單的岩石紙和剪刀遊戲。除了退出循環外,程序中的所有內容都正常工作。這裏是我的代碼:C++不會退出while while循環
/* 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();只是爲了防止程序一旦完成運行就立即退出。
如果我把其他所有東西都放下來,只留下那些影響它的代碼,這裏是我的。
char keep_going;
do
{
cout << "\n\nKeep playing?\n";
cin >> keep_going;
} while (keep_going = 'y');
它應該只能繼續並再次啓動循環,如果我特別輸入字母'y'但無論我輸入它只是似乎不能正常工作。預先感謝任何和所有幫助。
唉唉,哈哈,謝謝,我笨。只是其中一個簡單的語法錯誤。謝謝一堆!我會確保在未來尋找那種東西! –
它發生在我們身上。我記得上大學時,我的教授會建議我們改變比較順序,比如'42 == foo',這樣我們就不會遇到同樣的錯誤。它很有用,但對於英語用戶來說,這不是最直觀的。你的來電。 :) – Bucket
哈哈,我明白了,再次感謝。對此,我真的非常感激 :) –