getline/gets problem

I'm making this structure database program thingy here is the code

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
#include <conio.h>
#include <iostream>
#include <string>
using namespace 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


please help, really urgent :) tnx in advance
Last edited on
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: ";

This should solve your problems.
do you mean this?

1
2
3
4
 for(x=0;x<i;x++){
	cout<<"\nEnter student's last name: ";
    getline(cin, p[x].lastName);
	cout<<"Enter student's first name: ";


but as i said when i compile it, it gives a blank p[x].lastName

it will look like this

WELCOME TO THE STUDENT DATABASE PROGRAM

How many data do you want to input? 1

Enter student's last name: Enter student's first name: _

try it also

or maybe because im using DevC++? there sometimes is a difference

EDIT: I'll try this with Code::Blocks
I cant install visual studio because of lack of hard disk space
Last edited on
cin>>i; does not discard new line character, getline() tries to read the line and read empty line.

You can update the code in such a way:
1
2
3
do{
 getline(cin, p[x].lastName);
}while(p[x].lastName == "");
WOW!!! thank you very much, that solved my last problem,

thank you tfityo! :)
Topic archived. No new replies allowed.