2013-04-07 47 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