本文介绍了如何在使用getline()函数时从用户那里获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们我正面临一个问题
我已经使用cin输入了类似(cin>> ch)
的选项那么我曾经将输入字符串作为getline(cin,string_s)
但是当我使用cin时getline无法正常工作
如果我不使用cin,效果很好
为什么会发生??
并且还有其他选择可以采用整数,然后字符串输入.通过在cin>>
之后调用getline()
,您实际上只是在抓取cin>>
留下的换行符(和任何空白).
http://www.cplusplus.com/reference/string/getline/ [ ^ ]
您也可以使用getline()
来获取数字(只是需要采取额外的步骤才能将其解释为数字). >
using namespace std; string str; getline(cin, str); cout << str << endl;
hi friends im facing one problem
i have used cin to input a choice like (cin>>ch)
then i have used to take input to string as getline(cin, string_s)
but getline does not works when i use cin
if i donot use cin it works well
why it is happening???
and is there any alternative to take integer then string input
解决方案
这篇关于如何在使用getline()函数时从用户那里获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!