2016-10-04 98 views
0

這裏是一個新手。我有以下幾點:如何終止控制檯?

#include<iostream> 
#include<string> 
#include<iomanip> 
#include<cmath> 
#include<stdlib.h> 

using namespace std; 

int main() { 
    int choice; 
    double radius, length, width, base, height; 
    const double PI = 3.14159; 

    cout << ":Geometry Calculator:\n\n"; 
    cout << "1. Calculate the Area of a Circle \n"; 
    cout << "2. Calculate the Area of a Rectangle \n"; 
    cout << "3. Calculate the Area of a Triangle \n"; 
    cout << "4. Quit\n"; 
    cout << "4. Please pick number: " ; 
    cin >> choice; 

    switch (choice) { 
    case 1: 
     double aCirc; 
     cout << "Enter the RADIUS of your Circle: "; 
     cin >> radius; 
     aCirc = PI * pow(radius, 2); 
     cout << "The AREA of your CIRCLE IS: " << aCirc; 
     break; 
    case 2: 
     double aRec; 
     cout << "Enter the LENGTH and WIDTH of your Rectangle (seprate your numbers with a space): "; 
     cin >> length >> width; 
     aRec = length * width; 
     cout << "The AREA of your Rectangle is: " << aRec; 
     break; 
    case 3: 
     double aTri; 
     cout << "Enter the BASE and HEIGHT of your Triangle (separate your numbers with a space): "; 
     cin >> base >> height; 
     aTri = (base * height)/2; 
     break; 
    case 4: 
     exit(); 
    default: 
     cout << "\n You entered an Invalid Number GoodBye No Number" << endl; 
    } 


    return 0; 
} 

我試圖終止控制檯,而用戶按「4」我尋找各種可能的功能,但是我相信我利用它們不正確,我認爲這是正確的後,我在另一個網站看到它,但它沒有工作,任何人都可以幫忙?感謝您的時間和專業知識。

+1

您的意思是,當用戶按「4」程序終止,而不是當用戶按「4」和「返回」? –

+0

是的,這是對的,抱歉的混亂。 –

回答

0

一般來說,調用exit()是一種不好的做法,因爲如果您的應用程序包含較大的代碼,它會在嘗試調試並理解您的應用程序突然消失在空氣中的原因時給您帶來意想不到的行爲(假設您不記得所有代碼) 。

在您的示例中,我認爲調用break併發出日誌語句會更合適。

0

您可以使用的conio.h。它會返回一個可以轉換爲相應數字的字符。 getch()會阻止該程序,直到您按下某個鍵。同樣@PazO說,不要使用exit