c ++ getline()在调用多次时不会等待来自控制台的输入 [英] c++ getline() isn't waiting for input from console when called multiple times

查看:485
本文介绍了c ++ getline()在调用多次时不会等待来自控制台的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从控制台获取一些用户输入参数,两个字符串,两个int和一个双。我想使用的相关代码是:

  #include< string& 
#include< iostream>
using namespace std;

// ...

string inputString;
unsigned int inputUInt;
double inputDouble;

// ...

cout< Title:;
getline(cin,inputString);
tempDVD.setTitle(inputString);

cout<< Category:;
getline(cin,inputString);
tempDVD.setCategory(inputString);

cout<< 时间(分钟):;
cin>> inputUInt;
tempDVD.setDuration(inputUInt);

cout<< 年: ;
cin>> inputUInt;
tempDVD.setYear(inputUInt);

cout<< Price:$;
cin>> inputDouble;
tempDVD.setPrice(inputDouble);但是,当运行程序时,代替等待输入第一个inputString,代码doesn直到第二个getline()调用。因此,控制台输出如下所示:


标题:类别:


光标出现在类别后面。如果我现在输入,程序然后跳转到年输入,不允许我输入多个字符串。这里发生了什么?

解决方案

问题是你使用运算符混合调用getline / p>

请记住,运算符>>忽略前导空格,因此将正确地跨越行边界。但在成功检索到输入后停止读取,因此不会拖尾\\\
字符。因此,如果你在一个>>之后使用一个getline(),你通常会得到错误的东西,除非你仔细(首先删除未读的'\\\
'字符)。


$ b $诀窍是不使用两种类型的输入。



如果它是所有的数字(或者运算符>>),那么只需使用operator >>(注意字符串是是不与输入/输出对称的唯一基本类型(即不能很好地运行))。



如果输入包含字符串或东西的组合, ()然后只使用getline()并解析字符串中的数字。

  std :: getline ,line); 
std :: stringstream linestream(line);

int value;
linestream>>值;

//或者如果你有boost:
std :: getline(std :: cin,line);
int value = boost :: lexical_cast< int>(line);


I'm attempting to get a few user-input parameters from the console, two strings, two ints and a double. The relevant code I'm trying to use is this:

#include <string>
#include <iostream>
using namespace std;

// ...

string inputString;
unsigned int inputUInt;
double inputDouble;

// ...

cout << "Title: "; 
getline(cin, inputString);
tempDVD.setTitle(inputString);

cout << "Category: "; 
getline(cin, inputString);
tempDVD.setCategory(inputString);

cout << "Duration (minutes): "; 
cin >> inputUInt; 
tempDVD.setDuration(inputUInt);

cout << "Year: "; 
cin >> inputUInt; 
tempDVD.setYear(inputUInt);

cout << "Price: $"; 
cin >> inputDouble; 
tempDVD.setPrice(inputDouble);

However, when running the program, instead of waiting for the first inputString to be entered, the code doesn't stop until the second getline() call. Thus the console output looks like this:

Title: Category:

with the cursor appearing after category. If I input now, the program then jumps ahead to the year input, not allowing me to enter more than one string. What's happening here?

解决方案

The problem is you are mixing calls to getline() with the use of the operator >>.

Remember that operator >> ignored leading white space so will correctly continue across lines boundaries. But stops reading after the input has successfully been retrieved and thus will not swallow trailing '\n' characters. Thus if you use a getline() after a >> you usually get the wrong thing unless you are careful (to first remove the '\n' character that was not read).

The trick is to not use both types of input. Pick the appropriate one and stick to it.

If it is all numbers (or objects that play nice with operator >>) then just use operator >> (Note string is the only fundamental type that is not symmetric with input/output (ie does not play nicely)).

If the input contains strings or a combination of stuff that will require getline() then only use getline() and parse the number out of the string.

std::getline(std::cin, line);
std::stringstream  linestream(line);

int  value;
linestream >> value;

// Or if you have boost:
std::getline(std::cin, line);
int  value = boost::lexical_cast<int>(line);

这篇关于c ++ getline()在调用多次时不会等待来自控制台的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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