【问题标题】:Why do I need to press Enter twice after getline()?为什么 getline() 后需要按两次 Enter?
【发布时间】:2020-11-21 07:48:11
【问题描述】:

除了在cout<<"From Date: "; 之后到达getline() 之外,此程序中的一切都运行良好。在这一点上,无论我是输入还是直接按Enter而不输入,我都必须按两次Enter才能继续。我尝试删除cin.ignore(),但跳过第一个getline() 会导致更多问题。这是导致此问题的 sn-p-

int main() {
  Date d1;
  int choice;
  cout << "\nEnter choice: ";
  cin >> choice;
  cin.ignore(numeric_limits < streamsize > ::max(), '\n');
  switch (choice) {
  case 1:
    d1.diffbw();
    break;
  default:
    cout << "Wrong choice";
  }
  return 0;
}

void Date::diffbw() {
  Date current, ref;
  string choice;
  cout << "\n  Type 'man' to enter date manually else hit Enter to insert current date!";
  do {
    cout << "From Date:";
    getline(cin, choice);
    if (choice == "man")
      current.getdate(); //getdate() assigns day, month and year in object current
    else if (choice.empty())
      current = sysDate(); //sysDate returns system date
    else {
      cout << "\n  Wrong Choice!";
      cout << "\n  Enter Again?(y/n): ";
      getline(cin, choice);
    }
  } while (choice == "y" || choice == "Y");

  do {
    cout << "To Date:";
    getline(cin, choice);
    if (choice.empty())
      ref = sysDate();
    else if (choice == "man")
      ref.getdate();
    else {
      cout << "\n  Wrong Choice!";
      cout << "\n  Enter Again?(y/n): ";
      getline(cin, choice);
    }
  } while (choice == "y" || choice == "Y");

  current.calcAge(ref); //calcAge() calculates difference between two given dates.
  cout << "\n  Difference: ";
  cout << abs(current.day) << " day(s) " << abs(current.month) << " month(s) " << abs(current.year) << " year(s)";
}

P.S.- 我在 windows 上使用 g++ 编译器。

编辑:我在这里发布了整个函数,因为很多人难以理解这里的上下文。我还按照@john 的建议更正了“cin.ignore()”语法。我正在尝试计算两个给定日期之间的差异。

第二个“do while”循环没有任何错误,尽管它与第一个完全同义。

【问题讨论】:

  • 您错误地使用了cin.ignore。当您知道有要丢弃的输入时,应在 输入之后使用 ignore。当您无法知道输入流的状态时,不应在输入之前使用它。
  • 如果你使用Linux,你可以考虑使用GNU readline。然后输入行是可编辑的。否则,当您的程序在某个 pipeline 中时会发生什么?
  • 尝试在cout 行的末尾添加&lt;&lt; std::flush
  • 阅读getline
  • 请提供minimal reproducible example,这段代码之前你在做什么?

标签: c++ c++17 getline


【解决方案1】:

这可能是因为您首先尝试通过调用 cin.getline() 来获取一个值,然后程序完成但正在等待确认,并通过您按 Enter 获得确认。

基本上

Enter Value: 5 //Presses enter
Press any key to exit window.

【讨论】:

  • 程序显然没有在这里完成。 getline() 后面有一个 if 块,因此它应该继续执行。不应该吗?
  • @PracticalMinds:我们不理解你的程序,因为你没有提供任何minimal reproducible example。在许多操作系统上,getline 是一个阻塞输入操作。在 Linux 上,您可以考虑使用 poll(2) 来等待用户输入的有限时间
  • @BasileStarynkevitch 抱歉我的错误,我刚刚加入了 stackoverflow。我会尝试更多地了解他们。
【解决方案2】:

你的陈述

cout<<"\n\n  From Date:";

很奇怪,可能是错误的,因为std::cout 流通常是缓冲的。

您应该使用std::cout &lt;&lt; "From date:" &lt;&lt; std::endl; 并阅读良好的C++ programming book 并检查this C++ reference 您是否正确使用了C++ 标准库中的每个函数或特性。

如果您使用最近的GCC 进行编译,我建议启用所有警告和调试信息,因此使用g++ -Wall -Wextra -g 进行编译。然后使用您的调试器,例如GDB.

如果您的计算机运行一些 Linux 或 POSIX 系统,您可能有兴趣使用 GNU readline:然后输入行变为用户可编辑的。您可能还想使用Qt 编写图形应用程序。

这个程序一切正常

你是怎么检查的?你用的是什么调试器?你有什么测试用例?您是否尝试输入带有空格的内容,例如today's date 作为用户输入?发生了什么?

【讨论】:

  • 我尝试更正 cout 语法,但这无济于事。我不知道如何使用调试器,但很快就会尝试学习。我知道我的程序运行良好,因为我手动测试了几乎所有的案例(我知道的)。我在 Windows 10 上使用 g++ 编译器。
  • 是的,我尝试了两个带空格的参数,并且正确地重定向到显示“错误选择”的 else 部分。但这一次它不需要额外的回车键。
【解决方案3】:

我稍微修改了您的程序以避免依赖您未定义的 Date 类:

#include <string>
#include <iostream>
#include <limits>
#include <math.h>
#include <stdio.h>

using namespace std;
void diffbw();

int main() {
  int choice;
  cout << "\nEnter choice: ";
  cin >> choice;
  cin.ignore(numeric_limits < streamsize > ::max(), '\n');
  switch (choice) {
  case 1:
    diffbw();
    break;
  default:
    cout << "Wrong choice";
  }
}

void diffbw() {
  string choice;
  cout << "\n  Type 'man' to enter date manually else hit Enter to insert current date!";
  do {
    cout << "From Date:";
    getline(cin, choice);
    cout << "choice";
    if (choice == "man") {
    }
    else if (choice.empty()) {
    }
    else {
      cout << "\n  Wrong Choice!";
      cout << "\n  Enter Again?(y/n): ";
      getline(cin, choice);
    }
  } while (choice == "y" || choice == "Y");

  do {
    cout << "To Date:";
    getline(cin, choice);
    if (choice.empty()) {
    }
    else if (choice == "man") {
    }
    else {
      cout << "\n  Wrong Choice!";
      cout << "\n  Enter Again?(y/n): ";
      getline(cin, choice);
    }
  } while (choice == "y" || choice == "Y");

  cout << "\n  Difference: ";
}

我没有看到您描述的相同现象。也就是说,我不需要按两次 Enter。请考虑让有问题的程序更精简,同时更完整。

【讨论】:

  • 谢谢,下次再考虑。
  • 我可以从这段代码中推断出问题在于'if'和'else if'块函数,即'getdate()'和'sysDate()',但我尝试替换'getline ()' 与 'cin>>' 在原始代码中,它解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2022-07-12
相关资源
最近更新 更多