输入后程序退出 [英] Program exits after taking input

查看:78
本文介绍了输入后程序退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究C ++ Primer Plus,由于Visual Studio 2017出现了一些怪异的问题,因此我正在使用Visual Studio Code应对其中的编程挑战.我看了很多其他类似的文章,但是那里的解决方案没有不为我工作.例如,我尝试在main()函数的右花括号处放置一个断点,并在返回0之前将cin.ignore()放在末尾.但是这些都不起作用.这是代码.

I have been going through C++ Primer Plus and I am working on the programming challenges in it using Visual Studio Code since there were weird issues with Visual Studio 2017. I have looked at a lot of other similar posts but the solutions there don't work for me. For example I tried putting a breakpoint at the main() function's closing curly brace, putting in cin.ignore() at the end before return 0. But none of that works. Here is the code.

#include <iostream>

using namespace std;

int main() {
    int inInput;
    cout << "Enter your height in inches." << endl;
    cin >> inInput;
    int feet = inInput/12;
    int inches = inInput%12;
    cout << feet << inches << " is your height." << endl;
    cin.ignore();
    return 0;

}

尝试了将其标记为重复且不起作用的解决方案.

Tried the solution that this was marked as a duplicate of and it didn't work.

推荐答案

我尝试启动您的代码,并在最后一行(返回之前)放了cin.get();行. 之后,我的窗口没有关闭.

I tried to launch your code and as last line (before return) I putted cin.get(); line. After that my window are not closing.

此外,您可以再添加一行cin.ignore();或将现有的一行更改为cin.ignore(2),这也将为您提供帮助. 之所以关闭,是因为在执行此行之后

Also, you may add one more line of cin.ignore(); or change existing one to cin.ignore(2)and it will also help you. The reason why it closing, because after this line executed

cin >> inInput;

still \n在输入缓冲区内部.因此,首先cin.ignore()仅忽略一个\n并以其自然方式关闭控制台应用程序.

still \n is inside input buffer. So first cin.ignore() is only ignoring one \n and shut down console applciation by its natural way.

p.s.还有几种等待方式:

p.s. A few more ways to wait:

这只是控制台应用程序的默认模式:完成执行程序后关闭.

It is just default mode for console application: close after finishing executing program.

希望它会有所帮助!祝你好运!

Hope it will help! Good luck!

这篇关于输入后程序退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆