gpt4 book ai didi

c++ - 从开关C++退出

转载 作者:行者123 更新时间:2023-12-01 15:05:25 28 4
gpt4 key购买 nike

#include <iostream>
#include <string>
#include <vector>
#include "Player.h"
using namespace std;

void PlayerMenu();


int main() {


int z;
cout << "Please press 0 to see the PLayers Menu. " << endl;
cin >> z;
while (z == 0) {
PlayerMenu();

}

cout << " Now You're Functional Lets get started. ";


};
void PlayerMenu()
{
char ch;
int num;

do {
system("cls");
cout << "\n\n\n\t Player Menu";
cout << "\n\n1 Wallet Balance ";
cout << "\n\n2 Player Invetory";
cout << "\n\n3 To Exit";
cin >> ch;
system("cls");
switch (ch)
{
case '1':
cout << "Your Balance at the moment is ..."<<endl;
cout << "\n";
Bank();
break;

//Show Wallet Balance
case '2':

cout << "Here is your Inventory"<<endl;
cout << "\n";
break;

//Show Inventory

case '3':
cout << " Bye.\n";
break;
//exit i'VE TRIED bREKA BUT it will not go back to the main source code or main method
}

cin.ignore();
cin.get();


} while (ch != '3');//If not 1 or 2 or 3 will ignore it
}

我尝试了break语句,但是break方法不会退出到main方法并运行最后的以下语句。我也想在案例之间运行方法,因此当玩家选择1时,它将显示玩家的余额。同样,当玩家输入值2时,它将显示购买武器的 vector 。

最佳答案

使用return而不是break退出当前功能。然后,您不需要while (ch != '3')。相反,您可以使用无限循环:

while (true) {
// ...

case '3':
cout << " Bye.\n";
return;
}

cin.ignore();
cin.get();
}

您也可以使用 for (;;)而不是 while (true),但这只是一种样式选择。

另外,不要在main的循环中调用 PlayerMenu()。做就是了:
int main()
{
int z;
cout << "Please press 0 to see the PLayers Menu. " << endl;
cin >> z;
if (z == 0) {
PlayerMenu();
}

cout << " Now You're Functional Lets get started. ";
}

关于c++ - 从开关C++退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58942816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com