简体   繁体   English

C ++,cin.get(); 不工作

[英]C++, cin.get(); not working

I don't understand when I run this in Visual Studio 2013 why the run window goes away. 我不明白在Visual Studio 2013中运行此窗口时为什么会消失。 So to compensate for that I put in a cin.get(); 因此,为了弥补这一点,我放入了cin.get();。 but it's still not working. 但仍然无法正常工作。 Can someone explain me what I'm doing wrong and how to fix it? 有人可以向我解释我在做什么错以及如何解决吗? Mind it, I'm very new to C++. 请注意,我是C ++的新手。

#include <iostream>
using namespace std;

int main()
{
    int a = 0;

    cout << "How old are you? \n";
    cin >> a;
    cout << a;
    cin.get();
    return 0;

} 

When your input is a number, the line 当您输入的是数字时,该行

cin >> a;

reads the number and leaves the newline character in the input stream. 读取数字并将换行符留在输入流中。 When the line 当行

cin.get();

is executed, the newline character is read and discarded. 执行后,将读取并删除换行符。 Hence, the program doesn't wait for any further input. 因此,该程序无需等待任何进一步的输入。 It executes the next line, returns from main and the program finishes. 它执行下一行,从main返回,程序完成。

您可以在cout之后使用system("PAUSE")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM