如何终止控制台?

时间:2016-10-04 03:51:20

标签: c++ math switch-statement terminate

这是一个新手。我有以下内容:

#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”后立即终止控制台我搜索了不同的可能功能但是我相信我正在使用它们不正确,我认为这是正确的,我在另一个站点看到它然而它确实不工作,谁能帮忙?感谢您的时间和专业知识。

2 个答案:

答案 0 :(得分:0)

一般来说,调用exit()是一种不好的做法,因为如果您的应用程序包含更大的代码,那么在尝试调试时会出现意外行为并理解为什么您的应用程序突然消失(假设您不记得所有代码)用心))。在你的样本中,我认为调用break并发出日志声明会更合适。

答案 1 :(得分:0)

您可以使用getch() conio.h。它将返回一个可以转换为等效数字的字符。 getch()阻止程序,直到你按下一个键。同样@PazO说,不要使用exit