在while循环的第二次迭代中跳过Cin

时间:2016-06-20 16:55:57

标签: c++ while-loop cin

嘿伙计们我一直在努力解决这个问题三个小时似乎没什么用。程序运行时,我输入Jay的号码进入管理菜单,除了添加员工选项外,一切正常。当我选择它时,它第一次运行正常,然后一个员工创建管理菜单,弹出所有选项并闪烁每隔几秒钟就像刷新一样。在当前状态下,我在所有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()的信息都将不胜感激!

1 个答案:

答案 0 :(得分:1)

试试这些:

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

如果有效,请告诉我。