【问题标题】:Why do I never get the chance to enter my input?为什么我从来没有机会输入我的输入?
【发布时间】:2018-09-24 06:58:24
【问题描述】:

当我想输入年份后的地址时,编译器不会等待我的回答

#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;
}

【问题讨论】:

  • 请注明这是什么语言。
  • 只需复制粘贴您的代码。它工作正常。你能详细说明错误吗?
  • 请将所有 '\n' 替换为 endl 并检查。
  • 它正在工作!使用 g++ Linux
  • 你能再检查一遍吗?

标签: c++ input terminal


【解决方案1】:

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

请优先使用endl 而不是\n

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

等价于

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

【讨论】:

    【解决方案2】:

    你不要让窗口打开,试试类似的东西

    char c;
    cin >> c;
    return 0;
    

    在 main 函数的末尾,这样您的终端将保持打开状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多