Getline问题 [英] Getline problem

查看:64
本文介绍了Getline问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

system("CLS");
string title;
string content;
cout << "Get title." << endl;
getline(cin,title);
cout << "Get content." << endl;
getline(cin,content);

问题是-应用程序未询问标题,我已经获得标题,获取内容,然后等待用户输入,获得标题后不等待用户输入.b是否必须添加任何中断或提示?
或者,也许不是从用户输入中读取整个文本行的最佳主意吗?

The problem is - application is not asking about tittle, I've got Get title, get content and then waiting for user input, it's not waiting for user input after get title.bDo I have to add any break or smth?
Or maybe, that isn't the best idea to read whole text line from user input?

推荐答案

如果您有 cin>>东西; 调用要先于 system()调用.

If you have a cin >> something; call prior to your system() call.

例如,将输入取为整数.当 cin>>myintvar; (或类似名称),然后将整数放置在 myintvar 中,并且在流中一起发送'\ n'. getline 向上拾取 \ n 来表示输入行的末尾,因此实际上是跳过"了.

For example, taking input into an integer. When cin >> myintvar; (or similar) then the integer is placed in myintvar and the '\n' gets sent along in the stream. The getline picks the \n up as indicative of the end of a line of input, so it is effectively "skipped".

cin>> 更改为 getline()或调用 cin.ignore()来获取'\ n'(或更好的是,调用 cin.ignore(std :: numeric_limits< std :: streamsize> :: max(),'\ n'); 刷新输入缓冲区-但请确保您没有在过程中丢弃有价值的输入).

Either change the cin >> to a getline() or call cin.ignore() to grab the '\n'(or better, call a cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n' ); to flush the input buffer-- but be sure you're not throwing away valuable input in the process).

这篇关于Getline问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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