为什么在C++中无效选择后’break’不起作用?

huangapple go评论60阅读模式
英文:

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 &lt;&lt; &quot;Out of &quot; &lt;&lt; player1.GetNumGames() &lt;&lt; &quot; match(es): &quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Players tied &quot; &lt;&lt; player1.GetPlayerTies() &lt;&lt; &quot; time(s).&quot; &lt;&lt; endl;
cout &lt;&lt; player1.GetName() &lt;&lt; &quot; at &quot; &lt;&lt; player1.GetLocation() &lt;&lt; &quot; has won &quot; &lt;&lt; player1.GetPlayerWins();
cout &lt;&lt; &quot; match(es) and lost &quot; &lt;&lt; player1.GetPlayerLosses() &lt;&lt; &quot; match(es). Their win percentage is &quot;;
if (player1.GetNumGames() != 0) {
cout &lt;&lt; fixed &lt;&lt; setprecision(0) &lt;&lt; ((player1.GetPlayerWins() / player1.GetNumGames()) * 100) &lt;&lt; &quot;%&quot; &lt;&lt; endl;
}
else {
cout &lt;&lt; &quot;0%&quot; &lt;&lt; endl;
}
cout &lt;&lt; player2.GetName() &lt;&lt; &quot; at &quot; &lt;&lt; player2.GetLocation() &lt;&lt; &quot; has won &quot; &lt;&lt; player2.GetPlayerWins();
cout &lt;&lt; &quot; match(es) and lost &quot; &lt;&lt; player2.GetPlayerLosses() &lt;&lt; &quot; match(es). Their win percentage is &quot;;
if (player2.GetNumGames() != 0) {
cout &lt;&lt; fixed &lt;&lt; setprecision(0) &lt;&lt; ((player2.GetPlayerWins() / player2.GetNumGames()) * 100) &lt;&lt; &quot;%&quot; &lt;&lt; endl;
}
else {
cout &lt;&lt; &quot;0%&quot; &lt;&lt; endl;
}
cout &lt;&lt; endl;

}

void AccessFile::PrintGameInfo(Player player) {
winner = player;

cout &lt;&lt; &quot;The players played &quot; &lt;&lt; winner.GetNumGames() &lt;&lt; &quot; round(s) and the winner is &quot; &lt;&lt; winner.GetName() &lt;&lt; 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 &lt;&lt; endl;
cout &lt;&lt; &quot;Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Enter player 1 choice: &quot; &lt;&lt; endl;
cin &gt;&gt; player1Choice;
while (valid != false) {
//End game if chosen
if (player1Choice == 3) {
valid = false;
break;
}
//Play game
else if (player1Choice &gt;= 0 &amp;&amp; player1Choice &lt; 3) {
//Enter player 2 choice
cout &lt;&lt; &quot;Enter player 2 choice: &quot; &lt;&lt; endl;
cin &gt;&gt; player2Choice;
//End game if chosen
if (player2Choice == 3) {
valid = false;
break;
}
//Play game
while (valid != false) {
if (player2Choice &gt;= 0 &amp;&amp; player2Choice &lt; 3) {
gameCount += 1;
// Tie
if (player1Choice == player2Choice) {
cout &lt;&lt; &quot;Players have tied.&quot; &lt;&lt; endl;
cout &lt;&lt; endl;
playerTies += 1;
valid = false;
}
//Player 1 wins
else if (player1Choice == 0 &amp;&amp; player2Choice != 1) {
cout &lt;&lt; player1.GetName() &lt;&lt; &quot; wins this match.&quot; &lt;&lt; endl;
cout &lt;&lt; endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 1 &amp;&amp; player2Choice != 2) {
cout &lt;&lt; player1.GetName() &lt;&lt; &quot; wins this match.&quot; &lt;&lt; endl;
cout &lt;&lt; endl;
player1Wins += 1;
valid = false;
}
else if (player1Choice == 2 &amp;&amp; player2Choice != 0) {
cout &lt;&lt; player1.GetName() &lt;&lt; &quot; wins this match.&quot; &lt;&lt; endl;
cout &lt;&lt; endl;
player1Wins += 1;
valid = false;
}
//Player 2 wins
else {
cout &lt;&lt; player2.GetName() &lt;&lt; &quot; wins this match.&quot; &lt;&lt; endl;
cout &lt;&lt; endl;
player2Wins += 1;
valid = false;
}
}
//Re-enter player 2 choice
else { 
cout &lt;&lt; &quot;Invalid entry. Try again.&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Enter player 2 choice: &quot; &lt;&lt; endl;
cin &gt;&gt; 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 &lt;&lt; &quot;Invalid entry. Try again.&quot; &lt;&lt; endl;
}
//Start new match
cout &lt;&lt; &quot;Enter 0 for rock, 1 for paper, 2 for scissors, 3 to quit&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Enter player 1 choice: &quot; &lt;&lt; endl;
cin &gt;&gt; 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&#39;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

huangapple
  • 本文由 发表于 2023年6月16日 15:27:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定