Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果语句不返回,则C++_C++_If Statement_Switch Statement - Fatal编程技术网

如果语句不返回,则C++

如果语句不返回,则C++,c++,if-statement,switch-statement,C++,If Statement,Switch Statement,当我输入b时,它将输出到命令行b和其他两行,然后返回主菜单,即“a”,如果命令字符等于“a”,为什么不返回主菜单?使用开关: 没有什么能告诉你的代码在第一个条件下返回 您可以执行以下操作: switch (command) { case 'a': ... break; case 'b': ... break; case 'c': ... break; default: break; } 类似这样的东西可能被认为是良好且清晰的编程风格:

当我输入b时,它将输出到命令行b和其他两行,然后返回主菜单,即“a”,如果命令字符等于“a”,为什么不返回主菜单?

使用开关:


没有什么能告诉你的代码在第一个条件下返回

您可以执行以下操作:

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

请注意,在switch语句之后,循环将从头开始,包括当EnterCommandLine函数完成执行时。

如果主菜单只是位于顶部的“cout”,然后是另一个cin,则有两种解决方案

您可能一直在考虑的解决方案是将代码示例包装在while循环中

在指挥时!=c{…}

一旦选择c,代码将结束。我假设如果玩家特别选择c,你不想返回主菜单

然而,对于当前的代码,我对这种方法并不十分满意,因为循环将继续检查更新的命令变量。如果您不想在同一个命令上无限循环,则必须将每个命令的命令状态设置回“a”。更好的解决方案是将代码分成函数

例如:

// 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;
}
注意:如果变量已经是变量“a”,则没有理由将命令更改为变量“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;
            }

        }
    }
} 

除非你有一个循环,否则你的程序从上到下执行一次。如果你想让这个选项运行不止一次,你会想把它包含在某种循环中。那么,如果我添加一个循环,If会在我转到b并输入a之后使用a返回If语句吗?或者更好的说法是回到开始
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;
            }

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