C++ 尝试在 getline 中使用 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to use int in getline
提问by Scott
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
getline(cin, totalquestions);
This small piece of code comes from a function in a class that I have created and I need totalquestions
to be an int so that it can run through a for loop and keep asking the total amount of questions that I have asked.
这一小段代码来自我创建的类中的一个函数,我需要totalquestions
是一个 int 以便它可以运行 for 循环并不断询问我所问的问题总数。
question q;
for(int i = 0; i < totalquestions; i++)
{
q.inputdata();
questions.push_back(q);
}
Where does this piece of code comes to play? Does anyone have any idea to make this work?
这段代码在哪里发挥作用?有没有人有任何想法使这项工作?
回答by sehe
Use
用
cin >> totalquestions;
Check the errors too
也检查错误
if (!(cin >> totalquestions))
{
// handle error
}
回答by James Kanze
getline
reads an entire line as a string. You'll still have
to convert it into an int:
getline
将整行读取为字符串。您仍然需要将其转换为 int:
std::string line;
if ( !std::getline( std::cin, line ) ) {
// Error reading number of questions...
}
std::istringstream tmp( line );
tmp >> totalquestions >> std::ws;
if ( !tmp ) {
// Error: input not an int...
} else if ( tmp.get() != EOF ) {
// Error: unexpected garbage at end of line...
}
Note that just inputting std::cin
directly into
totalquestions
does notwork; it will leave the trailing
'\n'
character in the buffer, which will desynchronize all of
the following input. It's possible to avoid this by adding a
call to std::cin.ignore
, but this would still miss the error
due to trailing garbage. If you're doing line oriented input,
stick with getline
, and use std::istringstream
for any
necessary conversions.
注意std::cin
直接输入
totalquestions
是不行的;它将'\n'
在缓冲区中保留尾随
字符,这将使以下所有输入不同步。可以通过添加对 的调用来避免这种情况std::cin.ignore
,但是由于尾随垃圾,这仍然会错过错误。如果您正在执行面向行的输入,请坚持使用getline
, 并std::istringstream
用于任何必要的转换。
回答by riwalk
Do this:
做这个:
int totalquestions;
cout << "How many questions are there going to be on this exam?" << endl;
cout << ">>";
cin >> totalquestions;
Getline is meant for grabbing chars
. It can be done with getline()
, but cin
is much easier.
Getline 用于抓取chars
. 它可以用 完成getline()
,但cin
要容易得多。
回答by dark_shade
One of the better ways of getting an int from user :-
从用户那里获取 int 的更好方法之一:-
#include<iostream>
#include<sstream>
int main(){
std::stringstream ss;
ss.clear();
ss.str("");
std::string input = "";
int n;
while (true){
if (!getline(cin, input))
return -1;
ss.str(input);
if (ss >> n)
break;
std::cout << "Invalid number, please try again" << std::endl;
ss.clear();
ss.str("");
input.clear();
}
Why is it better than using cin >> n ?
为什么它比使用 cin >> n 更好?
As for your question, use the above code to get the int value and then use it in the loop.
至于你的问题,使用上面的代码获取int值,然后在循环中使用。
回答by Fred Foo
Don't use getline
:
不要使用getline
:
int totalquestions;
cin >> totalquestions;