[英]Why do I need to press Enter twice after getline()?
除了在cout<<"From Date: ";
之后到达getline()
时,该程序中的一切正常cout<<"From Date: ";
. 在这一点上,无论是输入还是直接按回车,我都必须按两次回车才能继续。 我尝试删除cin.ignore()
但跳过第一个getline()
会导致更多问题。 这是导致此问题的代码段-
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)";
}
PS-我在 Windows 上使用 g++ 编译器。
编辑:我在这里发布了整个功能,因为很多人都难以理解这里的上下文。 我还按照@john 的建议更正了“cin.ignore()”语法。 我正在尝试计算两个给定日期之间的差异。
第二个“do while”循环没有任何错误,尽管它与第一个循环完全相同。
这可能是因为您首先尝试通过调用 cin.getline() 获取值,然后程序完成但正在等待确认,并且通过按 Enter 获得确认。
基本上
Enter Value: 5 //Presses enter
Press any key to exit window.
你的陈述
cout<<"\n\n From Date:";
很奇怪,可能是错误的,因为std::cout
流通常被缓冲。
你应该使用std::cout << "From date:" << std::endl;
并阅读一本优秀的C++ 编程书籍,并在此 C++ 参考中检查您是否正确使用了 C++ 标准库中的每个函数或特性。
如果您使用最新的GCC 进行编译,我建议启用所有警告和调试信息,因此使用g++ -Wall -Wextra -g
编译。 然后使用您的调试器,例如GDB 。
如果您的计算机运行某些 Linux 或 POSIX 系统,您可能对使用GNU readline感兴趣:然后输入行变为用户可编辑的。 您可能还想使用Qt编写图形应用程序。
在这个程序中一切正常
你是怎么检查的? 你用的什么调试器? 你有什么测试用例? 您是否尝试输入带空格的内容,例如today's date
作为用户输入? 发生了什么?
我稍微修改了您的程序以避免依赖您未定义的 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 两次。 请考虑使您的有问题的程序更小,同时更完整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.