getline

I write a program that uses struct to display address, I don't understand why the getline command work only when it is repeated at follow:


getline (cin,em.locate.street);getline (cin,em.locate.street); // working

getline (cin,em.locate.street); // not working and skipping to the next line (question)


You probably have a line-feed character still in the buffer from a previous read. Accessing the input using >> will not remove the line-feed character from when you pressed the 'return' key for the previous input.
what should I do in this case? just double the getline command and move on
Add cin.ignore(numeric_limits<streamsize>::max(), '\n') after >>
or
don't use >>. Use getline if you can.
Hamsterman,

I use getline
I often just use getline() to clear the line of spurious data.
@curioustoknow,
Your code is most likely is somethin like this:
1
2
3
4
5
6
7
8
9
10
11
int main(){
    //...
    string str;
    cin >> str;//a >> is causing the problem
    //solution 1 would be to add cin.ignore here
    //(which is better as it does not change program's behavior)
    //soution 2 would be to replace this with getline(cin, str);
    //...
    getline(cin, em.locate.street);
    //...
}


@Galik
What parameters do you use? Do you create a string for storing the rubbish? Seems kind of wasteful..
Last edited on
hamsterman wrote:
What parameters do you use? Do you create a string for storing the rubbish? Seems kind of wasteful..

It depends on the situation really. I wouldn't use getline() in every situation. But its not uncommon to have a general input string lying around while reading data.
@hamsterman.

my code was getline (cin,em.locate.street);

the problem is, if i put it in one time it will not work but if I do it twice it does;

the only way it will work is if i put like this:

getline (cin,em.locate.street); getline (cin,em.locate.street);


and I dont know why it will only work f the getline is repeated
Can you show us the code leading up to your getline()? Back to the previous read at least.
@curioustoknow,
You have more code than that one line..
Here are few examples:
1. works fine:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main(){
    std::string str;
    std::getline(std::cin, str);
    std::cout << str;

    std::cout << "\ndone!";
    std::cin.get();
    return 0;
}


2. skips the second read and prints an empty string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main(){
    std::string str;
    std::cin >> str;

    std::getline(std::cin, str);
    std::cout << str;

    std::cout << "\ndone!";
    std::cin.get();
    return 0;
}


3. works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <limits>

int main(){
    std::string str;
    std::cin >> str;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::getline(std::cin, str);
    std::cout << str;

    std::cout << "\ndone!";
    std::cin.get();
    return 0;
}


@Galik,
I agree that there is often a free string lying around somewhere, but still, your method does a lot more than it should.
here is my entire code, it works only if I repeat the getline twice. I already comment the second line so you can understand what Im talking abouit.

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
92
93
94
95
96
97
98
99
100
101
102
 // This program demonstrates the use of structure variables.
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Address {

	string street, city, state;
	int zip;
	
};

struct EmployeeRec
{
   string name;         // Employee name
   string empId;        // Employee number
   double payRate;      // Hourly pay rate
   double hours;        // Hours worked
   double grossPay;     // Gross pay
   Address locate;
};



EmployeeRec GetEmployeeRec ();			 //Function Prototype 
void DisplayEmployeeRec (EmployeeRec);  //Function Prototype 


int main()
{

	EmployeeRec employee1, employee2 ; 
	
	cout<<"Employee # 1 info: "<<endl<<endl;  

	employee1 = GetEmployeeRec();

	cout<<endl<<endl<<"Employee # 2 info: "<<endl<<endl;

	employee2 = GetEmployeeRec();
	
   cout<<endl<<"                                    "<<endl;
   cout<<"----------------------------------- "<<endl<<endl;
   

   cout << fixed << showpoint << setprecision(2);

   DisplayEmployeeRec (employee1);
   cout<<"-----------------------------------"<<endl;
   DisplayEmployeeRec (employee2);
   cout<<"-----------------------------------";
   cout<<endl;

   return 0;
}

EmployeeRec GetEmployeeRec() {

EmployeeRec em;

	cout<<"please enter the employee name : ";
	cin>>em.name;

	cout<<"please enter the employee street address : ";
	getline (cin,em.locate.street); //getline (cin,em.locate.street);
	cout<<"please enter the employee city  : ";
	cin>>em.locate.city;
	cout<<"please enter the employee state  : ";
	cin>>em.locate.state;
	cout<<"please enter the employee zip code : ";
	cin>>em.locate.zip;

	cout<<"please enter the  employee ID: ";
    cin>>em.empId;
 
	cout<<"please enter the employee PayRate: ";
	cin>>em.payRate;

	cout<<"please enter the hours worked: ";
	cin>>em.hours;
	
return em;
}


void DisplayEmployeeRec (EmployeeRec em) {

cout << "Name: " <<em.name<< endl;
cout << "address: " <<em.locate.street<<" "<<em.locate.city<<" "<<em.locate.state<<" "<<em.locate.zip<<endl;
cout << "ID : "<<em.empId<< endl;
cout << "Pay Rate: "<<em.payRate<< endl;
   
cout << "Hours Worked: "<<em.hours<< endl;
em.grossPay = em.hours * em.payRate;
cout << "Gross Pay: " <<em.grossPay<< endl;
 
}




1
2
3
4
5
6
7
8
9
10
11
12
13
	cout<<"please enter the employee name : ";
	
	// Here is the problem. This line does not extract the end of line
	// marker that gets added to the input when the user presses the
	// return key.
	cin>>em.name;

	// So when you do a getline() it will get that end of line character
	// and stop there, because it has got everything up to end of line.
	
	// FIX: To fix this, change the above statement
	// to use getline(cin, em.name) and not cin >> em.name
Cool. Thanks a lot
Topic archived. No new replies allowed.