简体   繁体   English

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;
}
}

im trying to get it to when I enter b, it will output going to command line B and the other two lines and then return to the main menu, which is 'a', why is it not returning to the main menu if the command char equals 'a'? 当我输入b时,我试图将它输入,它将输出到命令行B和另外两行然后返回主菜单,这是'a',为什么它不返回主菜单如果命令char等于'a'?

Use a switch : 使用switch

switch (command) {
case 'a':
    ...
    break;
case 'b':
    ...
    break;
case 'c':
    ...
    break;
default: 
    break;
}

Well there's nothing telling your code to come back at the first condition. 好吧,没有什么可以告诉你的代码在第一个条件下回来了。

You could do the following: 您可以执行以下操作:

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;
}
}

If your main menu is just your 'cout' at the top followed by another cin, then there are two solutions. 如果您的主菜单只是顶部的'cout',然后是另一个cin,那么有两个解决方案。

The solution you may have been thinking of is to wrap your code sample in a while loop. 您可能一直在考虑的解决方案是将代码示例包装在while循环中。

(while command != "c"){ ... }

This will end the code once you select c. 一旦选择c,这将结束代码。 I'm assuming that you don't want to return to the main menu if the player chooses c specifically. 如果玩家专门选择c,我假设您不想返回主菜单。

With the current code I'm not exactly thrilled with this method however, as the loop will keep checking the updated command variable. 使用当前代码,我对此方法并不十分兴奋,因为循环将继续检查更新的command变量。 You will have to set the state of command back to 'a' for every command if you don't want to be infinitely looping on the same command. 如果您不想在同一命令上无限循环,则必须为每个命令将command状态设置回'a'。 A better solution would be to separate the code into functions. 更好的解决方案是将代码分离为函数。

as an example: 举个例子:

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 : there's no reason to change command to variable 'a' if the variable is already variable 'a': PS:如果变量已经是变量'a',则没有理由将命令更改为变量'a':

(your code) (你的代码)

case 'a':
            cout<<"Going to the main menu!"<<endl;
            command = 'a';
            break;

Something like this would probably be considered good and clear programming style: 像这样的东西可能被认为是好的和清晰的编程风格:

// 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;
}

Note that after the switch statement the loop will start again from the beginning, including when the EnterCommandLine functions finish execution. 请注意,在switch语句之后,循环将从头开始,包括EnterCommandLine函数完成执行时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM