在 while 循环的第二次迭代中跳过 Cin
Cin is skipped in the second iteration of while loop
大家好,我已经努力解决这个问题大约三个小时了,但似乎没有任何效果。当程序运行时,我输入 Jay 的号码以进入管理菜单,除了“添加员工”选项外,一切正常。当我 select 它第一次运行良好,然后创建员工时,弹出所有选项的管理菜单,并每隔几秒闪烁一次,就像刷新一样。在当前状态下,在所有 cin 语句之后我有一堆忽略,因为网上的每个人都说这与用户在输入后按下回车键然后在下一次迭代中被读取有关,但老实说我只是在猜测大约。
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
bool AdminMenu(int id);
int EmployeeMenu(int id);
void printInfo(int id);
int findEmptySpot();
void createNewEmployee(int x);
struct employee{
string name;
bool adminStatus;
int id;
};
typedef struct employee Employee;
Employee data[100];
int main()
{
bool x = true;
while(x)
{
int input,i=0;
data[0] = {"Joshua",false,5656};
data[1] = {"Jay",true,1982};
cout<< "Enter your 4 digit id: ";
cin>> input;
cin.ignore();
std::system("cls");
while(data[i].id!=input)
i++;
;
if(data[i].adminStatus ==true)
x = AdminMenu(i);
else
EmployeeMenu(i);
}
return 0;
}
bool AdminMenu(int placeInArray)
{
while(true)
{
cin.ignore();
int input=0;
cout<<"You are in the admin menu\n1. Print My info\n2. Add Employee\n8. Close Program\n9. Finish"<<endl;
cin>> input;
if(input<1||input>9)
{
cout<< "Invalid input please try again"<<endl;
Sleep(1000);
std::system("cls");
}
else if(input==1)
{
std::system("cls");
printInfo(placeInArray);
getch();
std::system("cls");
}
else if(input ==2)
{
int newPlace = findEmptySpot();
cin.ignore();
cout<<" Enter the employee's name";
cin >> data[newPlace].name;
cin.ignore();
cout<<" Enter the employee's ID";
cin >> data[newPlace].id;
cin.ignore();
cout<<" Enter the employee's admin status";
cin >> data[newPlace].adminStatus;
std::system("cls");
cout<< "Sweet your new employee profile has been created!";
printInfo(newPlace);
}
else if(input ==8)
{
std::system("cls");
return false;
}
else if(input ==9)
{
std::system("cls");
return true;
}
}
}
int EmployeeMenu(int placeInArray)
{
int input;
while(true)
{
cout<< "You are in the Employee Menu\n1. Print My info\n9. Finish";
cin>> input;
std::system("cls");
if(input<1||input>9)
{
cout<< "Invalid input please try again"<<endl;
Sleep(1000);
std::system("cls");
}
else if(input==1)
printInfo(placeInArray);
else if(input ==9)
{
std::system("cls");
return 0;
}
}
}
void printInfo(int placeInArray)
{
cout<<"Name: "<<data[placeInArray].name<<endl;
cout<<"ID: "<<data[placeInArray].id<<endl;
cout<<"Admin status: "<< data[placeInArray].adminStatus<<""<<endl;
}
int findEmptySpot()
{
int i=0;
while(data[i].id!=0)
i++;
return i;
}
void createNewEmployee(int x)
{
}
无论如何,任何关于我需要在哪里使用 cin.clear() 和 cin.ignore() 的信息将不胜感激!
试试这些:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
如果有效请告诉我。
大家好,我已经努力解决这个问题大约三个小时了,但似乎没有任何效果。当程序运行时,我输入 Jay 的号码以进入管理菜单,除了“添加员工”选项外,一切正常。当我 select 它第一次运行良好,然后创建员工时,弹出所有选项的管理菜单,并每隔几秒闪烁一次,就像刷新一样。在当前状态下,在所有 cin 语句之后我有一堆忽略,因为网上的每个人都说这与用户在输入后按下回车键然后在下一次迭代中被读取有关,但老实说我只是在猜测大约。
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
bool AdminMenu(int id);
int EmployeeMenu(int id);
void printInfo(int id);
int findEmptySpot();
void createNewEmployee(int x);
struct employee{
string name;
bool adminStatus;
int id;
};
typedef struct employee Employee;
Employee data[100];
int main()
{
bool x = true;
while(x)
{
int input,i=0;
data[0] = {"Joshua",false,5656};
data[1] = {"Jay",true,1982};
cout<< "Enter your 4 digit id: ";
cin>> input;
cin.ignore();
std::system("cls");
while(data[i].id!=input)
i++;
;
if(data[i].adminStatus ==true)
x = AdminMenu(i);
else
EmployeeMenu(i);
}
return 0;
}
bool AdminMenu(int placeInArray)
{
while(true)
{
cin.ignore();
int input=0;
cout<<"You are in the admin menu\n1. Print My info\n2. Add Employee\n8. Close Program\n9. Finish"<<endl;
cin>> input;
if(input<1||input>9)
{
cout<< "Invalid input please try again"<<endl;
Sleep(1000);
std::system("cls");
}
else if(input==1)
{
std::system("cls");
printInfo(placeInArray);
getch();
std::system("cls");
}
else if(input ==2)
{
int newPlace = findEmptySpot();
cin.ignore();
cout<<" Enter the employee's name";
cin >> data[newPlace].name;
cin.ignore();
cout<<" Enter the employee's ID";
cin >> data[newPlace].id;
cin.ignore();
cout<<" Enter the employee's admin status";
cin >> data[newPlace].adminStatus;
std::system("cls");
cout<< "Sweet your new employee profile has been created!";
printInfo(newPlace);
}
else if(input ==8)
{
std::system("cls");
return false;
}
else if(input ==9)
{
std::system("cls");
return true;
}
}
}
int EmployeeMenu(int placeInArray)
{
int input;
while(true)
{
cout<< "You are in the Employee Menu\n1. Print My info\n9. Finish";
cin>> input;
std::system("cls");
if(input<1||input>9)
{
cout<< "Invalid input please try again"<<endl;
Sleep(1000);
std::system("cls");
}
else if(input==1)
printInfo(placeInArray);
else if(input ==9)
{
std::system("cls");
return 0;
}
}
}
void printInfo(int placeInArray)
{
cout<<"Name: "<<data[placeInArray].name<<endl;
cout<<"ID: "<<data[placeInArray].id<<endl;
cout<<"Admin status: "<< data[placeInArray].adminStatus<<""<<endl;
}
int findEmptySpot()
{
int i=0;
while(data[i].id!=0)
i++;
return i;
}
void createNewEmployee(int x)
{
}
无论如何,任何关于我需要在哪里使用 cin.clear() 和 cin.ignore() 的信息将不胜感激!
试试这些:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
如果有效请告诉我。