0

Am having trouble with cin >> after cin.getline()

In this code i must enter sex twice. I don't know what should ido to fix it. I had read a lot about cin.ignore() but can't solve my problem. Here is my code. ved[i] is a struct.

void stud ()
 {
cin.ignore () ; 
cout << "\n\n\nYou must enter information about five students\n\n\n ";
  for (int i = 0; i<5; i++)
    {
        system ("clear") ;

        cout << "-------------------------------------------------\n" ;
        cout <<"Enter information about  "<< (i+1) << " student \n";  
            { 
            cout<<"Enter name and surname\n"; 
             cin.getline(ved[i].name,150) ;

            }

           enter_sex :
             cout <<"Sex (1 - for male/2 - for female) : \n";

             int sex;
             cin >> sex; 
             while (!(cin >> sex) || (cin.peek() != '\n'))
                {
                cin.clear();
                while (cin.get() != '\n');
                        {
                            cout << "\n\nYou must enter 1 OR 2!\nTry again\n\n" << endl;
                            goto enter_sex;
                        }
                } 
                switch (sex)
                {

                    case 1:
                    {
                        ved[i].sex[0] ='m' ;
                        break;
                    }
                    case 2:
                    {
                        ved[i].sex[0] = 'f' ;
                        break;
                    }

                }

Thanks for help.

0 Answers0