获取线 CIN 忽略输入中的第一个数字
getline cin ignoring first number from input
我现在正在学习C++,我目前正在尝试使用 cin 和 getline 获取输入。但是,getline以某种方式忽略了输入中的数字。我尝试放置cin.clear()和cin.ignore(),但问题仍然存在。我做错了什么吗?
这是我的代码:
string test;
int main()
{
std::cout << "Please enter a date: ";
std::cin >> test;
std::getline(std::cin, test);
cout << test << endl;
}
这是输出:
Please enter a date: 1 January 2015
January 2015
Press any key to continue . . .
除非你想读一些东西,否则不要使用std::cin
。
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
int main()
{
string test; // Don't use global variable unless it is necessary.
std::cout << "Please enter a date: " << std::flush;
// std::cin >> test; // remove this harmful line
std::getline(std::cin, test);
cout << test << endl;
cout << "Press any key to continue . . ." << endl;
return 0;
}
>getline
不会追加。您正在读入字符串,直到它到达空格,然后用该行的其余部分覆盖它。
std::cin >> test; //test == "1"
std::getline(std::cin, test); //test == " January 2015"
相关文章:
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 为什么字符串流只读取整数中的第一个数字?如何使它只在空白处移动?
- 说单精度碰撞的第一个数字是 131072.02 是否正确?(阳性,将 2 位数字视为尾数)
- 如何在 c++ 中将第一个数字移动到数字的末尾
- C 搜索算法第一个数字= n
- 为什么我的数组只接受它们中的第一个数字
- 给定一个数字n,打印序列中大于或等于n的第一个数字
- 如何从文件中获取数字行并忽略第一个数字
- 打印由用户定义的数组,第一个数字确定单词的大小,第二个数字确定行的大小
- 两个非常大的数字的乘积的第一个数字
- for循环在将第一个数字放入数组后退出
- 查找双精度数组中的第一个数字
- 获取线 CIN 忽略输入中的第一个数字
- 默认值仅适用于 8 和 9.之后,它使用第一个数字并将其视为 1,2,3
- Atoi 仅返回 char* 参数的第一个数字
- 如何从C++文件中读取第一个数字
- c++中的随机数生成第一个数字不是随机的
- 如何打印输入的第一个数字?
- 计算文本文件每行的第一个数字