0

在下文中,我希望程序在function2中“条件”小于3时退出,但此时它只是在switch语句之后继续执行代码。我该如何实现我的目标?

#include <iostream>

using namespace std;

int main ()
{
    string answer;
    int selection;

    do
    {
        cin >> selection;

        switch(selection)
        {
            case 1:
            function1();
            break;

            case 2:
            function2();
            break;

            default:
            break;
        }

    cout << "Anything else?" << endl;
    cin >> answer;

    }while(!(answer == "no"));

    return 0;
}

void function1()
{   
    //do stuff
}

int function2()
{
    int condition;

    cin >> conditon;

    if(condition < 3)
    {
        cout << "That's an error." << endl;
        return 0;
    }
    return 0;
}
4

1 回答 1

1

包含<stdlib.h>在您的标题中并放入exit(0)而不是返回 0,这对我有用:)

于 2013-04-07T03:58:58.007 回答