每当我尝试运行此代码:计划被跳过函数getline()/ C++
string title;
int Choice;
cout<<"1. Insert new term ";
cin>>Choice;
if (Choice==1)
{
getline(cin,title);
}
程序只读的选择和结束的全过程:/,请帮助:d
每当我尝试运行此代码:计划被跳过函数getline()/ C++
string title;
int Choice;
cout<<"1. Insert new term ";
cin>>Choice;
if (Choice==1)
{
getline(cin,title);
}
程序只读的选择和结束的全过程:/,请帮助:d
cin>>Choice;
叶输入缓冲区中的尾随换行符。然后getline(cin,title);
因此读取一个空行。
通常,最好不要将格式化输入与来自同一个流的getline
混合。
一个快速简便的解决办法是使用std::basic_istream::ignore
删除从流的末尾换行符,就像这样:
cin.ignore(2, '\n');
这种说法
cin>>Choice;
输入缓冲区将包含新的生产线后,通过按Enter键留下字符。
所以遇到新行字符,直到与getline
if (Choice==1)
{
getline(cin,title);
下一条语句读取一个空字符串。此语句前
插入下面的调用
#include <limits>
//...
{
getline(cin,title);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
清除缓冲区。
非常感谢:D,它工作 –