我需要以下程序来获取整行用户输入并将其放入字符串名称:
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);
Run Code Online (Sandbox Code Playgroud)
然而,使用cin >> number命令之前的getline()命令(我猜这是问题),它不允许我输入名称.为什么?
我听说过一个关于cin.clear()命令的事情,但我不知道这是如何工作的,或者为什么这甚至是必要的.
小智 19
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);
Run Code Online (Sandbox Code Playgroud)
jon*_*sca 17
另一种方法是做一个
cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
Run Code Online (Sandbox Code Playgroud)
在你cin>>number;完全刷新输入缓冲区之后(拒绝所有额外的字符,直到找到换行符).你需要#include <limits>得到这个max()方法.
Ton*_*roy 10
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);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,这一点......
char c;
while (cin.get(c) && c != '\n')
if (!std::isspace(c))
{
std::cerr << "ERROR unexpected character '" << c << "' found\n";
exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)
...在数字只包含空格后检查输入行的其余部分.
这非常冗长,因此ignore在流后使用>> x是一种经常推荐的替代方法,可以将内容丢弃到下一个换行符,但它可能会丢弃非空白内容,并且这样做会忽略文件中的损坏数据.您可能会也可能不会关心,具体取决于文件的内容是否受信任,避免处理损坏数据的重要性等.
因此,std::cin.clear()(和std::cin.igore())不是必需的,但对于删除错误状态很有用.例如,如果您想给用户很多机会输入有效数字.
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());
}
Run Code Online (Sandbox Code Playgroud)
另一个简单但半生不熟的替代方案是ignore用于std::skipws在读取行之前跳过任何数量的空白...
if (std::cin >> number >> std::skipws)
{
while (getline(std::cin, name))
...
Run Code Online (Sandbox Code Playgroud)
...但是如果输入像"1E6"(例如某些科学家试图输入1,000,000但C++仅支持浮点数的符号)将不接受,那么你最终会number设置为1,并E6读为第一个价值name.另外,如果您有一个有效数字后跟一个或多个空行,那么这些行将被静默忽略.
尝试:
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;
Run Code Online (Sandbox Code Playgroud)
另外.如果您知道数字和名称将始终位于不同的行上.
cin >> number;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(cin, names);
Run Code Online (Sandbox Code Playgroud)
在使用getline之前,您可以使用std :: ws在输入缓冲区中提取任何空白字符.std :: ws的标题是sstream.
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
cin>>ws;
getline(cin, names);
Run Code Online (Sandbox Code Playgroud)
小智 5
在 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62256 次 |
| 最近记录: |