【发布时间】:2021-12-17 08:43:33
【问题描述】:
谁能向我解释为什么我的代码中的 getline() 语句没有像我预期的那样循环,我希望 while 循环中的代码永远执行但是后来我的代码只循环了代码,但跳过了 getline() 函数。我将提供屏幕截图...我的代码是:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
while(true)
{
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
cout << "Age: " << age << "\tName: " << name << "\n\n";
}
}
【问题讨论】:
标签: c++ data-structures while-loop cin getline