首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过cin.ignore()和cin.clear()清除输入流

无法通过cin.ignore()和cin.clear()清除输入流
EN

Stack Overflow用户
提问于 2020-06-09 18:15:43
回答 1查看 37关注 0票数 0

虽然我使用了cin.ignore()和cin.clear(),但我无法清除输入流,但它们在我的程序中都不起作用。如何修复它?请帮帮我,谢谢

代码语言:javascript
复制
#include<iostream>
#include<stdio.h>
using namespace std;
void Input_data(char name_of_student[], float &Score_of_Math, float &Score_of_Literature)
{


cout<<"Enter Name of Student: ";
cin.getline(name_of_student,1);
cin.ignore();
cout<<"Enter Score_of_Math: ";
cin >> Score_of_Math;
cout << "Enter Score_of_Literature: ";
cin >> Score_of_Literature;
cout << name_of_student;
}

int main()
{


char name_of_student[20];
float Score_of_Math;
float Score_of_Literature;
Input_data(name_of_student, Score_of_Math, Score_of_Literature);

}
EN

回答 1

Stack Overflow用户

发布于 2020-06-09 23:03:12

cin::getline的第二个参数表示字符数组最多只能接受多少个字符。该函数将自动在其中附加一个终止\0,因此您没有留下任何空间来加载任何有用的数据。该函数不从输入中提取任何内容,在这种情况下,它会将流上的失败位设置为真。您必须将该数字与数组的大小相匹配,并在它太小的情况下报告错误。

但您真正应该做的是将名称存储在std::string中。这将解决这个问题,并为其他bug留下更少的空间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62280215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档