[英]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和另外兩行然后返回主菜單,這是'a',為什么它不返回主菜單如果命令char等於'a'?
使用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;
}
}
如果您的主菜單只是頂部的'cout',然后是另一個cin,那么有兩個解決方案。
您可能一直在考慮的解決方案是將代碼示例包裝在while循環中。
(while command != "c"){ ... }
一旦選擇c,這將結束代碼。 如果玩家專門選擇c,我假設您不想返回主菜單。
使用當前代碼,我對此方法並不十分興奮,因為循環將繼續檢查更新的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;
}
}
}
}
PS:如果變量已經是變量'a',則沒有理由將命令更改為變量'a':
(你的代碼)
case 'a':
cout<<"Going to the main menu!"<<endl;
command = 'a';
break;
像這樣的東西可能被認為是好的和清晰的編程風格:
// 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
函數完成執行時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.