getline function prob

I am trying to input a line of data using getline(), but i have enter twice after each getline function. Any idea on it . Here is my source code below.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
#include<string>
using namespace std;

class plane
{
public:
	plane();
	~plane();
	void Setname();
	void Setnation();
	void Seticno();
	void Setaddress();
	string Getname();
	string Getnation();
	string Geticno();
	string Getaddress();
private:
	string Name,Nation,Icno,Address;
};

plane::plane(){}

plane::~plane(){}

void plane::Setname()
{
	getline(cin,Name);
}

void plane::Setnation()
{

	getline(cin,Nation);
}

void plane::Seticno()
{
	getline(cin,Icno);
}

void plane::Setaddress()
{
	getline(cin,Address);
}

string plane::Getname()
{
	return Name;
}

string plane::Getnation()
{
	return Nation;
}

string plane::Geticno()
{
	return Icno;
}

string plane::Getaddress()
{
	return Address;
}

void main ()
{
	plane s1[3];
	int i;

	for(i=1;i<=3;i++)
	{
	cout<<"Name        : ";
	s1[i].Setname();
	cin.ignore(1000,'\n');
	cout<<"Nationality : ";
	s1[i].Setnation();
	cin.ignore(1000,'\n');
	cout<<"I/C number  : ";
	s1[i].Seticno();
	cin.ignore(1000,'\n');
	cout<<"Address     : ";
	s1[i].Setaddress();
	cout<<" Check your details . "<<endl;
	cout<<"Name        : "<<s1[i].Getname()<<endl;
	cout<<"Nationality : "<<s1[i].Getnation()<<endl;
	cout<<"I/C number  : "<<s1[i].Geticno()<<endl;
	cout<<"Address     : "<<s1[i].Getaddress()<<endl;
	}
}


Name        : husdarin merah

Nationality : indian

I/C number  : 218946-32849263-6239

Address     : central of india

 Check your details .
Name        : husdarin merah
Nationality : indian
I/C number  : 218946-32849263-6239
Address     : central of india


Why i need to enter twice after each input to proceed to the next input?
PLease Help me out ....
cin.ignore(1000,'\n');
Don't need that for getline().
okie. but without the cin.ignore(1000,'\n') the output will be lik below.
Name        : husdarin merah

Nationality : indian
I/C number  : 1289563-3264
Address     : central of india
 Check your details .
Name        : husdarin merah
Nationality :
I/C number  : indian
Address     : 1289563-3264
Press any key to continue

why become lik tis one ? the output is totally unmatch with wat i input. Then i still have to enter twice after the 1st getline();
I removed all those cin.ignore() lines and it worked fine for me.. set up exactly how you wanted it to.

Other than that problem.. i've found that you've written out of an array's bounds -
plane s1[3]; for(int i = 1; i <= 3; i++)
You're accessing s1[3] when it ends at 2.
When you get to 3 in your for() loop, your program will break.
To fix this error, change your for() loop -
for(int i = 0; i < 3; i++) //access 0 first, then 1, then 2
Remember - array indexing starts at 0.
Last edited on
okie. May i know how to solve the problems above ?
Can you help me out of it ?
Fixed the probs. Act is not my source code prob. there is a bug on the getline for microsoft visual c++ 6.0 . So, anyone facing the same problem as me. Please kindly refer to this. http://www.edcc.edu/faculty/paul.bladek/getline_fix.htm
Topic archived. No new replies allowed.