如果在一些输入后使用getline()不工作 [英] getline() does not work if used after some inputs

查看:179
本文介绍了如果在一些输入后使用getline()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

需要帮助getline()

getline()不工作,如果我在一些输入后使用它,即

getline() is not working, if I use it after some inputs, i.e.

#include<iostream>
using namespace std;

main()
{
string date,time;
char journal[23];


cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;

cout<<"Journal Entry:\t";
cin.getline(journal,23);


cout<<endl;
system("pause");
}

其中就像使用 getline code>在输入之上,它的确工作即

where as if I use getline() on top of inputs, it does work i.e.

cout<<"Journal Entry:\t";
cin.getline(journal,23);
cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;

可能是什么原因?

推荐答案


字符被提取,直到(n - 1)个字符被
提取或者找到定界字符(如果指定了此$ ​​b $ b参数,则为delim,否则为'\\\
')。如果在输入序列中达到文件末尾或者如果在输入操作期间出现
错误,则提取还会停止

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.

cin.getline()从输入读取时,输入流中有一个换行符,因此它不会读取您的c-串。使用 cin.ignore() beore调用 getline()

When cin.getline() reads from the input, there is a newline character left in the input stream, so it doesn't read your c-string. Use cin.ignore() beore calling getline().

cout<<"Journal Entry:\t";
cin.ignore();
cin.getline(journal,23);

这篇关于如果在一些输入后使用getline()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆