提问人:pauliwago 提问时间:4/21/2011 最后编辑:Michael Curriepauliwago 更新时间:7/28/2020 访问量:98208
在 cin [duplicate] 之后使用 getline(cin, s)
Using getline(cin, s) after cin [duplicate]
问:
我需要以下程序来获取用户输入的整行并将其放入字符串名称中:
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);
但是,在命令之前使用命令(我猜这是问题所在),它不允许我输入名称。为什么?cin >> number
getline()
我听说过一些关于命令的事情,但我不知道这是如何工作的,也不知道为什么这是必要的。cin.clear()
答:
cout << "Enter the number: ";
int number;
if (cin >> number)
{
// throw away the rest of the line
char c;
while (cin.get(c) && c != '\n')
if (!std::isspace(c))
{
std::cerr << "ERROR unexpected character '" << c << "' found\n";
exit(EXIT_FAILURE);
}
cout << "Enter names: ";
string name;
// keep getting lines until EOF (or "bad" e.g. error reading redirected file)...
while (getline(cin, name))
...use name...
}
else
{
std::cerr << "ERROR reading number\n";
exit(EXIT_FAILURE);
}
在上面的代码中,这个位...
char c;
while (cin.get(c) && c != '\n')
if (!std::isspace(c))
{
std::cerr << "ERROR unexpected character '" << c << "' found\n";
exit(EXIT_FAILURE);
}
...在数字仅包含空格后检查输入行的其余部分。
为什么不直接使用忽略?
这非常冗长,因此在流之后使用是一种经常推荐的替代方法,可以丢弃内容到下一个换行符,但它可能会丢弃非空格内容,并且这样做会忽略文件中的损坏数据。您可能关心也可能不在乎,具体取决于文件的内容是否受信任,避免处理损坏数据的重要性等。ignore
>> x
那么什么时候会使用 clear 和 ignore?
因此,(和)不是必需的,但对于删除错误状态很有用。例如,如果要为用户提供许多输入有效号码的机会。std::cin.clear()
std::cin.ignore()
int x;
while (std::cout << "Enter a number: " &&
!(std::cin >> x))
{
if (std::cin.eof())
{
std::cerr << "ERROR unexpected EOF\n";
exit(EXIT_FAILURE);
}
std::cin.clear(); // clear bad/fail/eof flags
// have to ignore non-numeric character that caused cin >> x to
// fail or there's no chance of it working next time; for "cin" it's
// common to remove the entire suspect line and re-prompt the user for
// input.
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}
使用 skipws 或类似产品难道不能更简单吗?
另一个简单但半生不熟的替代方法是在阅读行之前跳过任意数量的空格......ignore
std::skipws
if (std::cin >> number >> std::skipws)
{
while (getline(std::cin, name))
...
...但是,如果它得到像“1E6”这样的输入(例如,一些科学家试图输入 1,000,000,但 C++ 只支持浮点数的表示法)不会接受这一点,你最终会设置为 ,并读取为 的第一个值。另外,如果有一个有效数字,后跟一个或多个空行,则这些行将被静默忽略。number
1
E6
name
评论
getline
clear
另一种方法是将一个
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
之后完全刷新输入缓冲区(拒绝所有多余的字符,直到找到换行符)。你需要得到方法。cin>>number;
#include <limits>
max()
评论
while (isspace(cin.peek())) cin.ignore()
尝试:
int number;
cin >> number;
char firstCharacterOfNames;
cin >> firstCharacterOfNames; // This will discard all leading white space.
// including new-line if there happen to be any.
cin.unget(); // Put back the first character of the name.
std::string names;
std::getline(cin, names); // Read the names;
或者。如果您知道该数字和名称将始终位于不同的行上。
cin >> number;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(cin, names);
cout << "Enter the number: ";
int number;
cin >> number;
cin.ignore(256, '\n'); // remaining input characters up to the next newline character
// are ignored
cout << "Enter names: ";
string names;
getline(cin, names);
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
// USE peek() TO SOLVE IT! ;)
if (cin.peek() == '\n') {
cin.ignore(1 /*numeric_limits<streamsize>::max()*/, '\n');
}
getline(cin, names);
return 0;
}
只需向前看一下,看看 a 是否仍然留在 的内部缓冲区中。如果是这样:忽略它(基本上跳过它)cin.peek()
'\n'
cin
评论
'\r'
在使用 getline 之前,可以使用 std::ws 提取输入缓冲区中的任何空格字符。std::ws 的标头是 sstream。
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
cin>>ws;
getline(cin, names);
您可以在 cpppreference 中找到您想要的答案。
当在空格分隔的输入之后立即使用时,例如,getline 使用 operator>> 在输入流上留下的结束行字符,并立即返回。一种常见的解决方案是在切换到面向行的输入之前,忽略输入行上的所有剩余字符。
int n; std::cin >> n;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
或者,您可以刷新输入缓冲区以读取字符串
法拉盛(stdin)
它在标头 stdio.h 中定义。
此代码有效。.
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
fflush(stdin); //FLUSHING STDIN
getline(cin, names);
您希望在 CIN 语句后使用 cin.ignore(),因为您想在使用 CIN 获取 int 变量后忽略缓冲区中剩余的“\n”。
我有一个类似的程序,我用过类似的问题:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "HackerRank ";
// Declare second integer, double, and String variables.
int n;
double d2;
string str;
// Read and save an integer, double, and String to your variables.
cin >> n;
cin >> d2;
cin.ignore();
getline(cin, str);
// Print the sum of both integer variables on a new line.
cout << i + n << endl;
// Print the sum of the double variables on a new line.
cout << d + d2 << endl;
// Concatenate and print the String variables on a new line
cout << s << str << endl;
// The 's' variable above should be printed first.
return 0;
}
我刚刚用过
getline(cin >> ws,lard.i_npute);
与标准
#include <iostream>
标头,在我遇到回车问题并且 ws 操纵器工作的情况下。我可能会开始将循环函数嵌入为类,并至少使用构造函数和析构函数调用。
从概念上讲,我认为你希望每个答案都是整齐的一行。那你为什么不试试这个呢?
cout << "Enter the number: ";
string line;
getline(cin, line);
int number = std::stoi(line);
cout << "Enter names: ";
string names;
getline(cin, names);
代码正确使用第一个换行符,如果该行正确,则给出数字,如果行不正确,则引发异常。全部免费!
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);//works on the \n left behind
getline(cin, names);//continues and rewrites names
它非常不言自明,在使用的流中留下了一个\n,它在第一次使用时被分配给名称。现在,重用 getline 会写入正确的值。cin >> number
在 getline() 函数之前使用 cin 时尝试 cin.ignore()
void inputstu(){
cout << "Enter roll Number:";
cin >> roll_no;
cin.ignore(); //ignore the withspace and enter key
cout << "Enter name:";
getline(cin, stu_name);
}
评论
5<enter>John<enter>
cin >> number
getline(cin,name)