[英]C++ ignore and clear buffer
int position;
afile.open("EmployeeInfo.dat", ios::in | ios::binary);
char name[80];
cout << "\nEdit Employee Info" << endl;
cout << "---------------------" << endl;
cout << "New Employee Username: ";
cin.clear();
cin.ignore(100, '\n');
cin.getline(name, 80);
bool flag = true;
hi all this is a code fragment of my program that i'm currently doing. 大家好,这是我当前正在执行的程序的代码片段。 most of the functionality are fine, with the exception that i need to press Enter twice to receive the getline data.
大部分功能都很好,但我需要按两次Enter键才能接收getline数据。 can anyone point out to me where im doing wrongly?
谁能指出我做错了什么?
last input prior to this is a integer input using cin >> choice; 在此之前的最后一个输入是使用cin >>选择的整数输入; going into a switch.
进入开关。 i do not have any errors with other functions even though they are doing the exact same thing.
即使其他功能完全相同,我也没有任何错误。 literally exact.
从字面上确切。
for eg. 例如 no errors with the following
以下没有错误
afile.open("EmployeeInfo.dat", ios::in | ios::binary);
char name[80];
cout << "\nAdd Employee Info" << endl;
cout << "---------------------" << endl;
cout << "New Employee Username: ";
cin.clear();
cin.ignore(100, '\n');
cin.getline(name, 80);
Okay here's the surrounding codes. 好的,这是周围的代码。
the function is called in another function 该函数在另一个函数中调用
cout << "Your Choice: ";
cin >> choice;
switch(choice)
{
case 1: cout << "Create Holiday Package" << endl;
break;
case 2: cout << "Delete Holiday Package" << endl;
break;
case 3: cout << "Edit Holiday Package" << endl;
break;
case 4: addEmployee(afile, num, e);
break;
case 5: delEmployee(afile, num, e);
break;
case 6: editEmployee(afile, num, e);
break; ....// to be continue case
}//end case
and this is the interaction i have with the cmd 这是我与cmd的交互
Please Enter Username: Daniel Roberts 请输入用户名:Daniel Roberts
Please Enter Password: Daniel1234 请输入密码:Daniel1234
Login successful 登陆成功
General Manager Menu 总经理菜单
1)Create Holiday Package 1)创建假期套餐
2)Delete Holiday Package 2)删除假期套餐
3)Edit Holiday Package 3)编辑假期套餐
4)Create a Staff Account 4)创建员工帐户
5)Delete a Staff Account 5)删除员工帐户
6)Edit a Staff Account 6)编辑员工帐户
7)View Stats of Booking Packages 7)查看预订套餐的统计信息
8)Quit 8)退出
Your Choice: 6 您的选择:6
Edit Employee Info 编辑员工信息
New Employee Username: John Smith 新员工用户名:John Smith
<- weird space here that i need to press enter to get rid of <-我需要按Enter摆脱的奇怪空间
Enter new name: 输入新名称:
The problem is the cin.ignore()
call, it will read until it has either read 100 characters or one of those characters are a newline. 问题是
cin.ignore()
调用,它将读取直到读取100个字符或这些字符之一是换行符为止。 So to satisfy it you have to enter eg a newline. 因此,要满足此要求,您必须输入例如换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.