gets function in loop

Im getting a problem in the gets function when i put it on a loop. The 1st phase of loop goes well but the second does not do. It skip the 1st cin. Im a beginner and i dont know what to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*A c++ program that will store user's input name and date 
 and will display the name if the user input dates are stored.  */
#include<iostream>
#define num 10
using namespace std;
int main()
{
    int i,m,d,y,t=0;
    struct tagname
    {
    char Firstname [30];
    char Lastname [30];
    int Month;
    int Date;
    int Year;
    }name[num];
    
    for(i=0;i<num;i++)
    {
     cout << "Enter Firstname: ";
     gets(name[i].Firstname);
     cout << "Enter Lastname: ";
     cin >> name[i].Lastname;
     cout << "Enter Month(Number Only): ";
     cin >> name[i].Month;
     cout << "Enter Date: ";
     cin >> name[i].Date;
     cout << "Enter Year: ";
     cin >> name[i].Year;
     
     cout << name[i].Firstname << " " << name[i].Lastname << endl << name[i].Month << "/"
          << name[i].Date << "/" << name[i].Year << endl;
     }
     cout << "Enter the Month: ";
     cin >> m;
     cout << "Enter the Date: ";
     cin >> d;
     cout << "Enter the Year: ";
     cin >> y;
     
     
     for(i=0;i<num;i++)
        if(m == name[i].Month)
        if(d == name[i].Date)
        if(y == name[i].Year)                
        {
             cout << name[i].Firstname << " " << "Has birthday in this Date." <<endl;
             t = 1;
        }
     if (t == 0)
        {
              cout << "Records doest match to your input" << endl;
        }
     system("pause");
     }

This is the output


Enter Firstname: Auric
Enter Lastname: Aleuric
Enter Month(Number Only): 11
Enter Date: 12
Enter Year: 95
Auric Aleuric
11/12/95
Enter Firstname: Enter Lastname: 
Do not mix operator>> and getline()/gets().
operator>> skips whitespace character from the beginning and leaves them at the end including linefeed.
gets() reads all character to linefeed, which in your case will be first symbol it encountered.
You simply need to clear the unwanted newline character from the input buffer, using cin.ignore();

http://www.cplusplus.com/forum/beginner/92711/#msg498253
Topic archived. No new replies allowed.