#include <conio.h>
#include <iostream>
#include <string>
usingnamespace std;
struct student
{
string lastName;
string firstName;
int Age;
string StudentNumber;
string Course;
string Yearlevel;
};
int main()
{
int i,x;
cout<<"WELCOME TO THE STUDENT DATABASE PROGRAM\n\nHow many data do you want to input? ";
cin>>i;
student p[i];
for(x=0;x<i;x++){
cout<<"\nEnter student's last name: ";
cin>>p[x].lastName;
getline(cin, p[x].lastName);
cout<<"Enter student's first name: ";
cin>>p[x].firstName;
cout<<"Enter student's age: ";
cin>>p[x].Age;
cout<<"Enter student's Student Number: ";
cin>>p[x].StudentNumber;
cout<<"Enter student's course: ";
cin>>p[x].Course;
cout<<"Enter student's year level: ";
cin>>p[x].Yearlevel;
}
cout<<"\nYou have entered "<<i<<" Student Information:\n"<<endl;
for(x=0;x<i;x++){
cout<<"Student Lastname: "<<p[x].lastName<<endl;
cout<<"Student Firstname: "<<p[x].firstName<<endl;
cout<<"Student Age: "<<p[x].Age<<endl;
cout<<"Student Number: "<<p[x].StudentNumber<<endl;
cout<<"Student Course: "<<p[x].Course<<endl;
cout<<"Student Year Level: "<<p[x].Yearlevel<<endl;
cout<<"\n\n";
}
getch();
return 0;
}
the problem is that when i input a two word lastname/surname only the 2nd up to the last word will output
(example: input- dela cruz, output- cruz; input- dela dela cruz, output- dela cruz)
i tried removing the
cin>>p[x].lastName;
but what happens is that the program skips on asking for the last name
it will just print out the next cout statement and will give a blank input in the last name
cin >> input_string; reads one word into input_string (words are separated by spaces, new lines characters, etc). getline(cin, input_string); reads entire line to the the new line character.
When you use
1 2
cin>>p[x].lastName;
getline(cin, p[x].lastName);
you read first word of line into p[x].lastName, then overwrite it with the rest of line by calling getline(cin, p[x].lastName);
You were right when you tried to remove cin>>p[x].lastName;
Remove it once more and keep cout<<"\nEnter student's last name: ";