cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);
在getline()
命令之前使用cin >> number
命令(我猜这就是问题所在),它将不允许我输入名称。为什么?
我听说过cin.clear()
命令,但我不知道它的作用是什么,也不知道为什么需要使用它。
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);
在getline()
命令之前使用cin >> number
命令(我猜这就是问题所在),它将不允许我输入名称。为什么?
我听说过cin.clear()
命令,但我不知道它的作用是什么,也不知道为什么需要使用它。
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);
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
在执行cin>>number;
之后,需要完全刷新输入缓冲区(拒绝所有额外的字符直到找到换行符)。你需要#include <limits>
来获取 max()
方法。
while (isspace(cin.peek())) cin.ignore()
...我觉得这样挺好。关于上面提到的,我更担心用户误解输入格式要求,或者某些脚本生成的输入数据出现错误,但是由于被忽略了,所以错误的结果被成功处理了 - 这可能会导致他们信任错误的结果。如果输入不符合规范,最好让程序生成一个错误。 - Tony Delroycout << "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
函数从流中忽略到下一个换行符,这比较简洁,但存在丢失非空格内容的风险,可能会导致文件数据损坏而被忽略。视文件内容是否可靠以及避免处理损坏数据的重要性而定,您可能关心或不关心这一点。
因此,当需要清除错误状态时,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());
}
对于您最初的需求,另一个简单但不够完整的替代方案是使用std::skipws
在读取行之前跳过任意数量的空格...
if (std::cin >> number >> std::skipws)
{
while (getline(std::cin, name))
...
...但是如果输入像 "1E6" 这样的输入(例如,某些科学家试图输入 1,000,000,但 C++ 仅支持浮点数的这种表示法),C++ 不会接受它,您会得到 number
设置为 1
,而 E6
会被读取为 name
的第一个值。另外,如果您有一个有效的数字后面跟着一个或多个空行,则这些行将被静默地忽略。
getline
的调用会消耗数字后面的换行符,并循环直到找到一个非空行...这在我看来是已经解决了。OP的问题并没有问“为什么”问题发生了,但确实评论说他不确定为什么需要clear
- 某人两天前编辑了这个5年前的问题以添加它,从而大大改变了其主旨。 - Tony Delroy尝试:
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;
cout << "Enter names: ";
string names;
cin>>ws;
getline(cin, names);
void inputstu(){
cout << "Enter roll Number:";
cin >> roll_no;
cin.ignore(); //ignore the withspace and enter key
cout << "Enter name:";
getline(cin, stu_name);
}
标签。
getline(cin >> ws,lard.i_npute);
使用标准
#include <iostream>
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
这很容易理解,cin >> number
留下了一个 \n 在流中,第一次使用时被分配给了 names。现在重新使用 getline 就写入了正确的值。
或者您可以刷新输入缓冲区以读取字符串
fflush(stdin)
它在头文件stdio.h中定义。
这段代码有效。
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
fflush(stdin); //FLUSHING STDIN
getline(cin, names);
cout << "Enter the number: ";
string line;
getline(cin, line);
int number = std::stoi(line);
cout << "Enter names: ";
string names;
getline(cin, names);
代码可以正确消耗第一个换行符,如果该行正确则返回数字,否则会抛出异常。全部免费!
5<Enter>John<Enter>
。然后,cin >> number
只读取了数字 5,而将换行符(Enter)留在流中。因此,当尝试使用getline(cin,name)
读取名称时,它会一直读到行末。但请注意,这里有一个换行符准备好被读取(因此名称将为空(因为您没有在读取数字 5 后读取掉换行符)。如果您想在 >> 和 getline() 之间切换,则需要小心处理输入的末尾换行符。 - Martin York