C ++ if语句没有返回

时间:2015-01-30 01:41:06

标签: c++ if-statement switch-statement

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和其他两行,然后返回主菜单,这是&#39; a&#39;,为什么不是如果命令char等于&#39; a&#39;?

,则返回主菜单

4 个答案:

答案 0 :(得分:1)

使用switch

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

答案 1 :(得分:1)

那么没有什么可以告诉你的代码在第一个条件下回来。

您可以执行以下操作:

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

答案 2 :(得分:1)

如果您的主菜单只是顶部的'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;
            }

        }
    }
} 

P.S。 :如果变量已经是变量'a',则没有理由将命令更改为变量'a':

(您的代码)

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

答案 3 :(得分:0)

这样的东西可能被认为是好的和清晰的编程风格:

// 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函数完成执行时。