2013-04-07 104 views
0

在下面,我希望程序退出,如果“条件”在函数2中小于3,但目前它只是在switch语句后继续执行代码。我如何去实现我的目标?如何返回0并终止函数?

#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; 
} 
+0

'出口(0);'<需要15个字符> – ApproachingDarknessFish 2013-04-07 03:55:58

回答

1

包括在你的头<stdlib.h>并把exit(0),而不是返回0,这对我的作品:)

+3

应该是'#包括'和如果我们想要符合C++标准,'std :: exit(0)'。 – 2013-04-07 04:02:47