如何获得字符串和int的用户输入
How do I get user inputs for a string and then an int?
我有一个数据库类,它是一个将容纳许多对象的数组。该函数将接受用户的两个输入,其中包括字符串和int
例如:
std::cout << "Enter first name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::getline(std::cin, first_name);
std::cout << "Enter last name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::getline(std::cin, last_name);
std::cout << "Enter age: ";
std::cin >> age;
当我运行代码时,在输入姓氏后点击回车键后,它只会开始一行新行,在它要求输入年龄之前,我必须输入另一个输入。
我听说把getline和cin混在一起不好,最好用其中一种。我能做些什么来实现这一目标?今后的良好做法是什么?
编辑:我在最初搜索解决方案时添加了忽略,因为如果没有它们,代码就不会麻烦等待用户输入。输出为"输入名字:输入姓氏:">
第2版:决议。问题是,我在代码的早期使用了"cin>>"让用户输入一个int变量,并且需要第一个cin.ignore语句,但不需要另一个。没有包括代码的这一部分,因为我不知道这会影响它。对这一切来说仍然是新的,所以感谢大家的帮助!
您的std::cin::ignore
调用对您没有帮助。只有在未提取行末尾字符(>>
)的输入之后才需要它们。
std::string first_name;
std::string last_name;
int age;
std::cout << "Enter first name: ";
std::getline(std::cin, first_name); // end of line is removed
std::cout << "Enter last name: ";
std::getline(std::cin, last_name); // end of line is removed
std::cout << "Enter age: ";
std::cin >> age; // doesn't remove end of line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // this does
// input can proceed as normal
在std::cin >> age;
之后只需要std::cin::ignore
调用,因为这不会删除行尾字符,而std::getline
调用会删除。
根据std::basic_istream::ignore()
的文档,此函数的行为类似于未格式化的输入函数,这意味着如果缓冲区中没有可跳过的内容,它将进入块并等待用户输入。
在您的情况下,两个ignore
状态都不是必需的,因为std::getline()
不会在缓冲区中留下换行符。所以实际发生的是:
std::cout << "Enter first name: ";
/*your first input is skipped by the next ignore line because its going to block until
input is provided since there is nothing to skip in the buffer*/
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
/* the next getline waits for input, reads a line from the buffer and also removes the
new line character from the buffer*/
std::getline(std::cin, first_name);
std::cout << "Enter last name: ";
/*your second input is skipped by the next ignore line because its going to block until
input is provided since there is nothing to skip in the buffer*/
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
/* the next getline waits for input and this is why it seems you need to provide
another input before it ask you to enter the age*/
std::getline(std::cin, last_name);
您需要删除ignore
语句才能使其工作。您可能还想阅读我何时以及为什么需要在C++中使用cin.ignore()
我建议删除ignore
函数调用:
std::string name;
std::cout << "Enter name: ";
std::getline(cin, name);
unsigned int age;
std::cout << "Enter age: ";
std::cin >> age;
相关文章:
- C++如何通过用户输入删除列表元素
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- C ++奇怪的数组由用户输入
- 如何允许大写和小写用户输入?
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用用户输入变量制作二维数组?
- 用户输入字符串的文件附加问题..C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?