c++ - 为什么程序没有暂停就关闭?(C++)
问题描述
我编写了一个 C++ 程序来测试复合不等式/等式。
当我在 IDE 中运行它时,它会在关闭之前停止,但是当我实际编译并作为可执行文件运行它时,控制台会在我读取输出之前关闭,即使我在末尾专门放了一行来暂停它.
代码如下:
#include <iostream>
using namespace std;
int main()
{
int x;
int y;
int a;
int b;
int test1;
int equality;
int test2;
bool et1;
bool lt1;
bool mt1;
bool et2;
bool lt2;
bool mt2;
bool t1;
bool t2;
cout << "What would you like to do? \n 1) Test two conditions are met \n 2) test if one condition is met out of two \n";
cin >> equality;
cout << "what would you like to test for the first pair of numbers? \n 1) if x == y \n 2) if x < y \n 3) if x > y \n";
cin >> test1;
cout << "what would you like to test for the second pair of numbers? \n 1) if x == y \n 2) if x < y \n 3) if x > y \n";
cin >> test2;
cout << "Choose the first number to the first inequality/equality \n";
cin >> x;
cout << "Choose the second number to the first inequality/equality \n";
cin >> y;
cout << "Choose the first number to the second inequality/equality \n";
cin >> a;
cout << "Choose the second number to the second inequality/equality \n";
cin >> b;
if (test1 == 1 && x == y){
et1 = true;
}
else if (test1 == 2 && x < y) {
lt1 = true;
}
else if (test1 == 3 && x > y) {
mt1 = true;
}
else if (test2 == 1 && a == b) {
et2 = true;
}
else if (test2 == 2 && a < b) {
lt2 = true;
}
else if (test2 == 3 && a > b) {
mt2 = true;
}
if (lt1 == true || et1 == true || mt1 == true) {
t1 = true;
}
if (lt2 == true || et2 == true || mt2 == true) {
t2 = true;
}
if (equality = 1 && t1 == true && t2 == true) {
cout << "this compound and inequality is true";
}
else if (equality = 2 ) {
if (t1 == true || t2 == true) {
cout << "this compound or inequality is true";
}
cout << "this compound inequality is true";
}
std::cin.get();
return 0;
}
解决方案
您可以包含头文件“conio.h”并在您想要暂停程序时执行 getch() 调用
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"After printing this there will be a pause.Press any character to contine\n";
getch();
cout<<"Code ends\n";
return(0);
}
推荐阅读
- django - 首次登录后 HUE 撤销管理员访问权限的 SAML 身份验证
- mips - Mips 程序计算段落中每个字符的出现次数
- formatting - Altair 工具提示:有没有办法为长文本格式化框形?
- android - 适用于 Android 的 Square Mobile Web API 给出“不支持的 API 版本” 当前 API 的正确版本号是多少?
- cordova - 科尔多瓦平台添加:添加第二个平台删除现有插件和平台的节点模块
- ibm-doors - Doors dxl 创建模块之间的链接
- firebase - Firebase 分析事件 in_app_purchase 和 app_store_subscription_convert 是如何被触发的?
- vb.net - 未使用 vb 和 ms-access 为命令对象设置命令文本
- c# - EF Core 5:FreeText 无法将参数值从字符串转换为全文索引 Byte[] 列上的 Byte[]
- python - 使用 glob 读取多个 CSV 导致顺序错误