英文:
Why won't 'break' work after invalid choice in C++?
问题
以下是代码部分的翻译:
#ifndef PLAYERH
#define PLAYERH
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
class Player {
public:
string SetName(string playerName);
string SetLocation(string PlayerLocation);
int SetPlayerWins(int playerWins);
int SetPlayerLosses(double gameCount, int playerTies);
string GetName() const;
string GetLocation() const;
int GetPlayerWins() const;
int GetPlayerLosses() const;
int GetPlayerTies() const;
double GetNumGames() const;
private:
string name;
string location;
double games = 0;
int ties = 0;
int losses = 0;
int wins = 0;
int i;
};
#endif
#include "Player.h"
using namespace std;
string Player::SetName(string playerName) {
name = playerName;
return name;
}
string Player::SetLocation(string playerLocation) {
location = playerLocation;
return location;
}
int Player::SetPlayerWins(int playerWins) {
wins = playerWins;
return wins;
}
int Player::SetPlayerLosses(double gameCount, int playerTies) {
games = gameCount;
ties = playerTies;
losses = gameCount - wins - ties;
return losses;
}
string Player::GetName() const {
return name;
}
string Player::GetLocation() const {
return location;
}
int Player::GetPlayerWins() const {
return wins;
}
int Player::GetPlayerLosses() const {
return losses;
}
int Player::GetPlayerTies() const {
return ties;
}
double Player::GetNumGames() const {
return games;
}
#ifndef ACCESSFILEH
#define ACCESSFILEH
#include "Player.h"
class AccessFile {
public:
void PrintMatchInfo(Player player1, Player player2);
void PrintGameInfo(Player player);
private:
vector<string> players;
ifstream inFS;
ofstream outFS;
string name;
string fileInfo;
char nameList;
int i;
int count = 0;
Player winner;
};
#endif
#include "Player.h"
#include "AccessFile.h"
using namespace std;
void AccessFile::PrintMatchInfo(Player player1, Player player2) {
cout << "Out of " << player1.GetNumGames() << " match(es): " << endl;
cout << "Players tied " << player1.GetPlayerTies() << " time(s)." << endl;
cout << player1.GetName() << " at " << player1.GetLocation() << " has won " << player1.GetPlayerWins();
cout << " match(es) and lost " << player1.GetPlayerLosses() << " match(es). Their win percentage is ";
if (player1.GetNumGames() != 0) {
cout << fixed << setprecision(0) << ((player1.GetPlayerWins() / player1.GetNumGames()) * 100) << "%" << endl;
}
else {
cout << "0%" << endl;
}
cout << player2.GetName() << " at " << player2.GetLocation() << " has won " << player2.GetPlayerWins();
cout << " match(es) and lost " << player2.GetPlayerLosses() << " match(es). Their win percentage is ";
if (player2.GetNumGames() != 0) {
cout << fixed << setprecision(0) << ((player2.GetPlayerWins() / player2.GetNumGames()) * 100) << "%" << endl;
}
else {
cout << "0%" << endl;
}
cout << endl;
}
void AccessFile::PrintGameInfo(Player player) {
winner = player;
cout << "The players played " << winner.GetNumGames() << " round(s) and the winner is " << winner.GetName() << endl;
}
#include "Player.h"
#include "AccessFile.h"
using namespace std;
int main() {
int player1Choice;
int player2Choice;
double gameCount = 0;
int player1Wins = 0;
int player2Wins = 0;
int playerTies = 0;
Player player1;
Player player2;
AccessFile player;
bool valid = true;
//Enter player 1 choice
cout << endl;
cout << "Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit" << endl;
cout << "Enter player 1 choice: " << endl;
cin >> player1Choice;
while (valid != false) {
//End game if chosen
if (player1Choice == 3) {
valid = false;
break;
}
//Play game
else if (player1Choice >= 0 && player1Choice < 3) {
//Enter player 2 choice
cout << "Enter player 2 choice: " << endl;
cin >> player2Choice;
//End game if chosen
if (player2Choice == 3) {
valid = false;
break;
}
//Play game
while (valid != false) {
if (player2Choice >= 0 && player2Choice < 3) {
gameCount += 1;
// Tie
if (player1Choice == player2Choice) {
cout << "Players have tied." << endl;
cout << endl;
playerTies += 1;
valid = false;
}
//Player 1 wins
else if (player1Choice == 0 && player2Choice != 1) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 1 && player2Choice != 2) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 2 && player2Choice != 0) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
//Player 2 wins
else {
cout << player2.GetName() << " wins this match." << endl;
cout << endl;
player2Wins += 1;
valid = false;
}
}
//Re-enter player 2 choice
else {
cout << "Invalid entry. Try again." << endl;
cout << "Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit" << endl;
cout << "Enter player 2 choice: " << endl;
cin >> player2Choice;
if (player2Choice == 3) {
valid = false;
break;
}
}
}
//Print match result
player1.SetPlayerWins(player1Wins);
player1.SetPlayerLosses(gameCount, playerTies);
player2.SetPlayerWins(player2Wins);
player2.SetPlayerLosses
<details>
<summary>英文:</summary>
I wrote this code for a rock, paper, scissors game. I think this is all of the relevant code, including the classes used.
#ifndef PLAYERH
#define PLAYERH
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
class Player {
public:
string SetName(string playerName);
string SetLocation(string PlayerLocation);
int SetPlayerWins(int playerWins);
int SetPlayerLosses(double gameCount, int playerTies);
string GetName() const;
string GetLocation() const;
int GetPlayerWins() const;
int GetPlayerLosses() const;
int GetPlayerTies() const;
double GetNumGames() const;
private:
string name;
string location;
double games = 0;
int ties = 0;
int losses = 0;
int wins = 0;
int i;
};
#endif
#include "Player.h"
using namespace std;
string Player::SetName(string playerName) {
name = playerName;
return name;
}
string Player::SetLocation(string playerLocation) {
location = playerLocation;
return location;
}
int Player::SetPlayerWins(int playerWins) {
wins = playerWins;
return wins;
}
int Player::SetPlayerLosses(double gameCount, int playerTies) {
games = gameCount;
ties = playerTies;
losses = gameCount - wins - ties;
return losses;
}
string Player::GetName() const {
return name;
}
string Player::GetLocation() const {
return location;
}
int Player::GetPlayerWins() const {
return wins;
}
int Player::GetPlayerLosses() const {
return losses;
}
int Player::GetPlayerTies() const {
return ties;
}
double Player::GetNumGames() const {
return games;
}
#ifndef ACCESSFILEH
#define ACCESSFILEH
#include "Player.h"
class AccessFile {
public:
void PrintMatchInfo(Player player1, Player player2);
void PrintGameInfo(Player player);
private:
vector<string> players;
ifstream inFS;
ofstream outFS;
string name;
string fileInfo;
char nameList;
int i;
int count = 0;
Player winner;
};
#endif
#include "Player.h"
#include "AccessFile.h"
using namespace std;
void AccessFile::PrintMatchInfo(Player player1, Player player2) {
cout << "Out of " << player1.GetNumGames() << " match(es): " << endl;
cout << "Players tied " << player1.GetPlayerTies() << " time(s)." << endl;
cout << player1.GetName() << " at " << player1.GetLocation() << " has won " << player1.GetPlayerWins();
cout << " match(es) and lost " << player1.GetPlayerLosses() << " match(es). Their win percentage is ";
if (player1.GetNumGames() != 0) {
cout << fixed << setprecision(0) << ((player1.GetPlayerWins() / player1.GetNumGames()) * 100) << "%" << endl;
}
else {
cout << "0%" << endl;
}
cout << player2.GetName() << " at " << player2.GetLocation() << " has won " << player2.GetPlayerWins();
cout << " match(es) and lost " << player2.GetPlayerLosses() << " match(es). Their win percentage is ";
if (player2.GetNumGames() != 0) {
cout << fixed << setprecision(0) << ((player2.GetPlayerWins() / player2.GetNumGames()) * 100) << "%" << endl;
}
else {
cout << "0%" << endl;
}
cout << endl;
}
void AccessFile::PrintGameInfo(Player player) {
winner = player;
cout << "The players played " << winner.GetNumGames() << " round(s) and the winner is " << winner.GetName() << endl;
}
#include "Player.h"
#include "AccessFile.h"
using namespace std;
int main() {
int player1Choice;
int player2Choice;
double gameCount = 0;
int player1Wins = 0;
int player2Wins = 0;
int playerTies = 0;
Player player1;
Player player2;
AccessFile player;
bool valid = true;
//Enter player 1 choice
cout << endl;
cout << "Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit" << endl;
cout << "Enter player 1 choice: " << endl;
cin >> player1Choice;
while (valid != false) {
//End game if chosen
if (player1Choice == 3) {
valid = false;
break;
}
//Play game
else if (player1Choice >= 0 && player1Choice < 3) {
//Enter player 2 choice
cout << "Enter player 2 choice: " << endl;
cin >> player2Choice;
//End game if chosen
if (player2Choice == 3) {
valid = false;
break;
}
//Play game
while (valid != false) {
if (player2Choice >= 0 && player2Choice < 3) {
gameCount += 1;
// Tie
if (player1Choice == player2Choice) {
cout << "Players have tied." << endl;
cout << endl;
playerTies += 1;
valid = false;
}
//Player 1 wins
else if (player1Choice == 0 && player2Choice != 1) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 1 && player2Choice != 2) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 2 && player2Choice != 0) {
cout << player1.GetName() << " wins this match." << endl;
cout << endl;
player1Wins += 1;
valid = false;
}
//Player 2 wins
else {
cout << player2.GetName() << " wins this match." << endl;
cout << endl;
player2Wins += 1;
valid = false;
}
}
//Re-enter player 2 choice
else {
cout << "Invalid entry. Try again." << endl;
cout << "Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit" << endl;
cout << "Enter player 2 choice: " << endl;
cin >> player2Choice;
if (player2Choice == 3) {
valid = false;
break;
}
}
}
//Print match result
player1.SetPlayerWins(player1Wins);
player1.SetPlayerLosses(gameCount, playerTies);
player2.SetPlayerWins(player2Wins);
player2.SetPlayerLosses(gameCount, playerTies);
player.PrintMatchInfo(player1, player2);
}
//Re-enter player 1 choice
else {
cout << "Invalid entry. Try again." << endl;
}
//Start new match
cout << "Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit" << endl;
cout << "Enter player 1 choice: " << endl;
cin >> player1Choice;
valid = true;
}
}
Everything works perfectly, except when you input an invalid number for player2, and then input 3 to quit right after, it won't quit. This is the output.
```none
Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit
Enter player 1 choice:
0
Enter player 2 choice:
4
Invalid entry. Try again.
Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit
Enter player 2 choice:
3
Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit
Enter player 1 choice:
It should look like this:
Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit
Enter player 1 choice:
0
Enter player 2 choice:
4
Invalid entry. Try again.
Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit
Enter player 2 choice:
3
The players have played 0 round(s) and they tied.
Originally I didn't have the second if branch for player2 to choose 3 beneath the invalid choice option. I added that because choosing an invalid choice, then choosing 3 to quit, came up with invalid choice again instead of quitting. The only thing that the change did was to make it skip back to the beginning to player1 choice. Choosing 3 the first time does cause it to quit. And choosing an invalid choice and then 3 for player1 also causes it to quit. I've run out of ideas.
答案1
得分: 2
你有两个层次的 while 循环。 break 只会跳出内层。 - BoP 19 分钟前 删除了第二个不必要的 while 循环。问题解决了。
英文:
You have two levels of while-loops. A break will only get out of the inner level. –
BoP
19 mins ago
Removed the second unnecessary while loop. Problem is solved
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论