首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么程序不暂停就关闭?(C++)

为什么程序不暂停就关闭?(C++)
EN

Stack Overflow用户
提问于 2019-11-03 01:38:25
回答 1查看 85关注 0票数 2

我已经写了一个C++程序来测试复合不等式/等式。

当我在IDE中运行它时,它会在它关闭之前停止,但当我实际编译并作为可执行文件运行它时,控制台在我可以读取输出之前就关闭了,即使我特意在末尾放了一行来暂停它。

代码如下:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-03 03:14:41

您可以包含头文件"conio.h“,并在需要暂停程序时执行getch()调用

代码语言:javascript
复制
#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);
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58673598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档