2013-08-31 103 views
1

例如,如果我做了一個簡單的開關箱,我將如何根據輸入使控制檯應用程序關閉?我不想用break來使用;並跳過循環,我想完全關閉控制檯。如何使用開關盒在C中退出控制檯應用程序?

char choice; 

printf("Run random function \n"); 
printf("Exit \n"); 

choice = getchar(); 
fflush(stdin); 

switch(choice) 
{ 

      case '1': 
       //randomFunction(); 

      case '2': 
       //I want this case to exit the console the console 
} 
+1

**出口(0); **會是你的選擇。 – Gangadhar

+0

'fflush(stdin);'是依賴於執行的 –

+0

@GrijeshChauhan更糟糕的是:這是未定義的行爲!例如見[這裏](http://stackoverflow.com/questions/2979209/using-fflushstdin) –

回答

0

你可以稱之爲exit()傳遞的返回值作爲參數,這個返回值可以通過調用進程/批處理文件來檢查:

exit(EXIT_SUCCESS); // = 0 = (standard) success 

exit(EXIT_FAILURE); // = 1 = (standard) failure 

exit(123); // return a specific value 

Documentation for MS-Visual Stuidio

1

只需使用exit (EXIT_SUCCESS);

參考 - exit

+0

這很好,非常感謝你!對不起,問一個隨機問題,我試着尋找年齡,但我只發現了C++或c#的結果 –

相關問題