This question already has answers here:
Preventing console window from closing on Visual Studio C/C++ Console application
(19个回答)
How to stop C++ console application from exiting immediately?
(35个答案)
去年关闭。
我最近正在尝试一些用于计算器的代码,但发现一个可以工作的代码。
但是无论我如何尝试,该程序都会在控制台上显示答案后立即关闭。请对此提供帮助,我已尽力使它停止。但这行不通...
我正在使用Visual Studio进行编码,如果与之有关,请告知我
(19个回答)
How to stop C++ console application from exiting immediately?
(35个答案)
去年关闭。
我最近正在尝试一些用于计算器的代码,但发现一个可以工作的代码。
但是无论我如何尝试,该程序都会在控制台上显示答案后立即关闭。请对此提供帮助,我已尽力使它停止。但这行不通...
我正在使用Visual Studio进行编码,如果与之有关,请告知我
#include <iostream>
#include <string>
#include <cctype>
#include<conio.h>
int expression();
char token() {
char ch;
std::cin >> ch;
return ch;
}
int factor() {
int val = 0;
char ch = token();
if (ch == '(') {
val = expression();
ch = token();
if (ch != ')') {
std::string error = std::string("Expected ')', got: ") + ch;
throw std::runtime_error(error.c_str());
}
}
else if (isdigit(ch)) {
std::cin.unget();
std::cin >> val;
}
else throw std::runtime_error("Unexpected character");
return val;
}
int term() {
int ch;
int val = factor();
ch = token();
if (ch == '*' || ch == '/') {
int b = term();
if (ch == '*')
val *= b;
else
val /= b;
}
else std::cin.unget();
return val;
}
int expression() {
int val = term();
char ch = token();
if (ch == '-' || ch == '+') {
int b = expression();
if (ch == '+')
val += b;
else
val -= b;
}
else std::cin.unget();
return val;
}
int main(int argc, char **argv) {
try {
std::cout << expression();
}
catch (std::exception &e) {
std::cout << e.what();
}
return 0;
}
最佳答案
通常,最好的方法是从命令解释器运行程序。我使用cmd.exe
。在我看来,当今的大多数程序员都更喜欢Powershell,但我讨厌它(对我来说就像COBOL)。您也可以使用Cygwin,以获得类似bash-shell的体验。我不建议在Windows 10开发人员模式下使用beta bash外壳程序:它很不稳定,如果您不太谨慎的话可能会做坏事。
在Visual Studio中,只需通过Ctrl + F5即可运行该程序,而无需调试即可运行该程序。
要从VS内部进行调试,可以在main
的最后右花括号上放置一个断点。
关于c++ - 如何使此C++计算程序停留在控制台上? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48135903/