C++ 使用enter键时循环不退出程序

C++ 使用enter键时循环不退出程序,c++,while-loop,conditional,C++,While Loop,Conditional,我正在为我正在为一个学校项目编写的头文件和实现文件编写一个主程序文件。我已经让我的项目做了我想做的一切,除了当用户点击“返回”按钮时退出 这是我的程序,包含以下主要功能: #include <iostream> #include "date.h" #include <string> using namespace std; int main() { dateType date1(1, 6, 2000); dateType date2(15, 20,

我正在为我正在为一个学校项目编写的头文件和实现文件编写一个主程序文件。我已经让我的项目做了我想做的一切,除了当用户点击“返回”按钮时退出

这是我的程序,包含以下主要功能:

#include <iostream>
#include "date.h"
#include <string>

using namespace std;

int main()
{

    dateType date1(1, 6, 2000);
    dateType date2(15, 20, 2018);
    dateType date3(2, 30, 2019);
    dateType date4;

    date1.printDate();
    cout << endl;
    date2.printDate();
    cout << endl;
    date3.printDate();
    cout << endl;

    string test;

    int month, day, year;

    cout << "Enter month day year: " << endl;
    cin >> month >> day >> year;

    date4.setDate(month, day, year);
    date4.printDate();
    cout << endl;

    cin.clear();
    cout << "press any key to continue... ";

    while (getline(cin, test)) {
    if (test.empty()) 
        break;

    }
    return 0;
}
#包括
#包括“date.h”
#包括
使用名称空间std;
int main()
{
dateType date1(1,6,2000);
日期类型date2(2018年12月15日至20日);
日期类型date3(2019年2月30日);
dateType date4;
date1.printDate();
年份;
日期4.设置日期(月、日、年);
date4.printDate();

cout您在这里的意图不清楚,但我将尝试回答它,首先,要将标准输入传递到程序中,必须按下enter键,至少在Linux上是这样。在控制台应用程序中,按键侦听器不是一个东西,除非有一个精心设计的第三方库生成一个侦听按键的工作线程使用背景

剩下的就是测试输入是否为空或包含任何数据。我看不出您的代码有任何问题,我创建了一个经过测试、缩小和验证的示例

#include <iostream>
#include <string>

using namespace std;

int main() {
  cout << "press any key to continue... ";

  string test;
  while (getline(cin, test)) {
    cout << test << test.length();  // For debugging purposes
    if (test.empty())
      break;
  }
  return 0;
}
#包括
#包括
使用名称空间std;
int main(){

我是否希望程序等待用户输入值以退出。当前,我的程序在显示“按任意键继续…”后立即退出,而不实际等待用户按键。