如何终止控制台?
问题描述:
这里是一个新手。我有以下几点:如何终止控制台?
#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”我寻找各种可能的功能,但是我相信我利用它们不正确,我认为这是正确的后,我在另一个网站看到它,但它没有工作,任何人都可以帮忙?感谢您的时间和专业知识。
答
一般来说,调用exit()
是一种不好的做法,因为如果您的应用程序包含较大的代码,它会在尝试调试并理解您的应用程序突然消失在空气中的原因时给您带来意想不到的行为(假设您不记得所有代码) 。
在您的示例中,我认为调用break
并发出日志语句会更合适。
答
您可以使用的conio.h
。它会返回一个可以转换为相应数字的字符。 getch()会阻止该程序,直到您按下某个键。同样@PazO说,不要使用exit
您的意思是,当用户按“4”程序终止,而不是当用户按“4”和“返回”? –
是的,这是对的,抱歉的混乱。 –