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.
/*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
usingnamespace 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.