C++ if语句不返回
C++ if statements not returning
int main()
{
char command = 'a';
Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
case 'b':
cout<<"Going to command line B"<<endl;
command = 'b';
break;
case 'c':
cout<<"going to command line C"<<endl;
command = 'c';
break;
}
}
if(command == 'b')
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
command = 'a';
}
if (command == 'c')
{
cout<<"You made it to command line C"<<endl;
}
}
我试图在我输入b时得到它,它会输出转到命令行B和其他两行然后return到主菜单,即'a',为什么如果命令字符等于 'a',它不会 return 进入主菜单吗?
使用 switch
:
switch (command) {
case 'a':
...
break;
case 'b':
...
break;
case 'c':
...
break;
default:
break;
}
好吧,没有什么可以告诉您的代码在第一个条件下返回。
您可以执行以下操作:
while(true)
{
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
case 'b':
cout<<"Going to command line B"<<endl;
command = 'b';
break;
case 'c':
cout<<"going to command line C"<<endl;
command = 'c';
break;
}
}
if(command == 'b')
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
command = 'a';
}
if (command == 'c')
{
cout<<"You made it to command line C"<<endl;
}
}
像这样的东西可能会被认为是好的和清晰的编程风格:
// Function forward-declarations:
void EnterMainMenu();
void EnterCommandLineB();
void EnterCommandLineC();
int main()
{
Monster Goblin;
Goblin.HP = 5;
Goblin.name = "Goblin";
EnterMainMenu();
}
void EnterMainMenu()
{
while(true) // Infinite loop
{
char command;
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
// Main menu loop will start again after the next line
break;
case 'b':
cout<<"Going to command line B"<<endl;
EnterCommandLineB();
break;
case 'c':
cout<<"going to command line C"<<endl;
EnterCommandLineC();
break;
}
}
}
void EnterCommandLineB()
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void EnterCommandLineC()
{
cout<<"You made it to command line C"<<endl;
}
请注意,在 switch
语句之后,循环将从头开始,包括当 EnterCommandLine
函数完成执行时。
如果你的主菜单只是你的 'cout' 在顶部然后是另一个 cin,那么有两种解决方案。
您可能一直在考虑的解决方案是将您的代码示例包装在一个 while 循环中。
(while command != "c"){ ... }
一旦你 select c. 这将结束代码。我假设如果玩家专门选择 c,您不想 return 到主菜单。
使用当前代码,我对这种方法并不十分满意,因为循环将继续检查更新的 command
变量。如果您不想在同一命令上无限循环,则必须将 command
的状态设置回 'a' 以便 每个 命令。更好的解决方案是将代码分成函数。
举个例子:
void commandB (){
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void commandC (){
cout<<"You made it to command line C"<<endl;
}
int main()
{
char command = 'a';
//Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
while (command != 'c'){
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
break;
case 'b':
cout<<"Going to command line B"<<endl;
commandB();
command = 'a'; // THIS IS WHAT KEEPS YOU WITHIN THE MM!
break;
case 'c':
cout<<"going to command line C"<<endl;
commandC();
break;
}
}
}
}
P.S。 : 如果变量已经是变量 'a':
,则没有理由将命令更改为变量 'a'
(您的代码)
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
int main()
{
char command = 'a';
Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
case 'b':
cout<<"Going to command line B"<<endl;
command = 'b';
break;
case 'c':
cout<<"going to command line C"<<endl;
command = 'c';
break;
}
}
if(command == 'b')
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
command = 'a';
}
if (command == 'c')
{
cout<<"You made it to command line C"<<endl;
}
}
我试图在我输入b时得到它,它会输出转到命令行B和其他两行然后return到主菜单,即'a',为什么如果命令字符等于 'a',它不会 return 进入主菜单吗?
使用 switch
:
switch (command) {
case 'a':
...
break;
case 'b':
...
break;
case 'c':
...
break;
default:
break;
}
好吧,没有什么可以告诉您的代码在第一个条件下返回。
您可以执行以下操作:
while(true)
{
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
case 'b':
cout<<"Going to command line B"<<endl;
command = 'b';
break;
case 'c':
cout<<"going to command line C"<<endl;
command = 'c';
break;
}
}
if(command == 'b')
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
command = 'a';
}
if (command == 'c')
{
cout<<"You made it to command line C"<<endl;
}
}
像这样的东西可能会被认为是好的和清晰的编程风格:
// Function forward-declarations:
void EnterMainMenu();
void EnterCommandLineB();
void EnterCommandLineC();
int main()
{
Monster Goblin;
Goblin.HP = 5;
Goblin.name = "Goblin";
EnterMainMenu();
}
void EnterMainMenu()
{
while(true) // Infinite loop
{
char command;
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
// Main menu loop will start again after the next line
break;
case 'b':
cout<<"Going to command line B"<<endl;
EnterCommandLineB();
break;
case 'c':
cout<<"going to command line C"<<endl;
EnterCommandLineC();
break;
}
}
}
void EnterCommandLineB()
{
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void EnterCommandLineC()
{
cout<<"You made it to command line C"<<endl;
}
请注意,在 switch
语句之后,循环将从头开始,包括当 EnterCommandLine
函数完成执行时。
如果你的主菜单只是你的 'cout' 在顶部然后是另一个 cin,那么有两种解决方案。
您可能一直在考虑的解决方案是将您的代码示例包装在一个 while 循环中。
(while command != "c"){ ... }
一旦你 select c. 这将结束代码。我假设如果玩家专门选择 c,您不想 return 到主菜单。
使用当前代码,我对这种方法并不十分满意,因为循环将继续检查更新的 command
变量。如果您不想在同一命令上无限循环,则必须将 command
的状态设置回 'a' 以便 每个 命令。更好的解决方案是将代码分成函数。
举个例子:
void commandB (){
cout<<"You made it to command line B"<<endl;
cout<<"Now lets try to make it go back to the MM!"<<endl;
}
void commandC (){
cout<<"You made it to command line C"<<endl;
}
int main()
{
char command = 'a';
//Monster Goblin;Goblin.HP = 5;Goblin.name = "Goblin";
while (command != 'c'){
if(command == 'a'){
cout<<"At the main Menu, what to do now? Enter H for a list of commands!"<< endl;
cin>>command;
switch(command)
{
case 'a':
cout<<"Going to the main menu!"<<endl;
break;
case 'b':
cout<<"Going to command line B"<<endl;
commandB();
command = 'a'; // THIS IS WHAT KEEPS YOU WITHIN THE MM!
break;
case 'c':
cout<<"going to command line C"<<endl;
commandC();
break;
}
}
}
}
P.S。 : 如果变量已经是变量 'a':
,则没有理由将命令更改为变量 'a'(您的代码)
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;