#include <iostream>
#include <string>
int main()
{
using namespace std;
string name;
string dessert;
cout<<"Enter your name:
";
getline(cin,name);//存在问题,输入两次Enter键,才会运行后面一句cout语句,待解决
cout<<"Enter your favorite dessert:
";
getline(cin,dessert);
cout<<"I have some delicious "<<dessert;
cout<<" for you, "<<name<<".
";
system("pause");
return 0;
}
运行结果:
Enter your name:
Han Meimei<Enter>
<Enter>
Enter your favorite dessert:
Radish Torte<Enter>
<Enter>
I have some delicious Radish Torte for you, Han Meimei.
对于存在的getline需要输入两次Enter键才能输出后面一句的问题,解决办法:
修改string头文件,
找到 else if(_Tr::eq((_E)_C,_D))
{_Chg=true;
//_I.rdbuf()->snextc();/* 注释掉这句,修改为下一句*/
_I.rdbuf()->sbumpc();
break;}
这样原本的问题就解决了。
修改后运行结果为:
Enter your name:
Han Meimei<Enter>
Enter your favorite dessert:
Radish Torte<Enter>
I have some delicious Radish Torte for you, Han Meimei.