为什么我永远都没有机会输入我的输入?

时间:2018-09-24 06:58:24

标签: c++ input terminal

当我想在编译器不等待我回答的年份之后输入地址

#include <iostream>
#include <cstring>

using namespace std;
int main() 
{
    cout << "What year was your house built?\n";
    int year;
    cin >> year;
    cout << "What is its street address?\n";
    char address[80];
    cin.getline(address, 80);
    cout << "Year built: " << year << endl;
    cout << "Address: " << address << endl;
    cout << "Done!\n";
    return 0;
}

2 个答案:

答案 0 :(得分:0)

在这种情况下,控件不要等待您的输入,因为它很可能会选择流中的输入。为了克服这种情况,请在每个endl语句后使用cout

请更喜欢使用endl而不是\n

cout << "What is its street address?\n";

等同于

cout << "What is its street address?\n";
cout.flush();

答案 1 :(得分:-2)

您不要让窗口保持打开状态,尝试类似

char c;
cin >> c;
return 0;

在主功能末尾,因此您的终端将保持打开状态。