getline

Hello community
Have 1 problem with getline

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
#include "std_lib_facilities.h"
#include <iostream>
#include <string>
#include "Test.h"
using namespace std;
int main()
{
	string first_name, friend_name, mail_1,/* mail_2, mail_3,*/ sign_1, sign_2, skype, phone;
	char friend_sex = 0, f = 0, m = 0;
	int age;
	cout << "Please inpute name of receiver: " << endl;
	getline (cin, first_name);
	cin.clear();
	cin.sync();
	cout << "Please inpute mail: " << endl;
	getline (cin, mail_1);
	//getline (cin, mail_2);
	//getline (cin, mail_3);
	cin.clear();
	cin.sync();
	cout << "Please input friend name: " << endl;
	getline (cin, friend_name);
	cin.clear();
	cin.sync();
	cout << "What gender your friend is? Input f or m." << endl;
	cin >> friend_sex;
	cin.sync();
	cout << "Please input age of receiver: " << endl;
	cin >> age;
	cin.sync();
	cout << "Please input sign here: " << endl;
	cin >> sign_1 >> sign_2;
	cin.sync();
	cout << "Please input skype: " << endl;
	cin >> skype;
	cin.sync();
	cout << "Please input phone: " << endl;
	getline (cin, phone);
	//cin >> phone;
	cin.clear();
	cin.sync();
	cout << "Dear " << first_name << "," << endl
		<< mail_1 << endl
		//<< mail_2 << endl
		//<< mail_3 << endl
		<< "Did you see " << friend_name << " recently?" << endl;
	if (friend_sex == 'f') cout << "If you will see " << friend_name << " please tell her to call me." << endl;
	else if (friend_sex == 'm') cout << "If you will see " << friend_name << " please tell him to call me." << endl;
	if (age <= 0) simple_error("Are you kidding me?");
	else if (age >= 110) simple_error("Are you kidding me?");
	else cout << "I hear you just throw a birthday party. You turn " << age << endl;
	if (age == 12) cout << "Next year you will hit " << ++age << endl;
	else if (age == 18) cout << "You can vote next year!" << endl;
	else if (age > 60) cout << "I hope you don't boring at retirement." << endl;
	cout << "With best regards, " << sign_1 << " " << sign_2 << endl
		<< "Skype: " << skype << ";" << endl
		<< "Phone: " << phone << ";" << endl;
}


This one just skip line with getline (cin, phone);
http://i.imgur.com/SG3gZRd.jpg
Read below.
Last edited on
Formatted input such as cin >> skype; will leave at the very least, a trailing newline character in the input buffer. If the user typed anything else, such as a blank space before pressing enter, that will remain in the buffer too.

On the other hand getline() just reads everything up to the newline character (which is read and discarded).

To get them to work together, you need to empty the buffer after formatted input, using ignore().
cin.ignore() will ignore a single character. Below I specified 1000, as an example, so it will ignore up to that many characters, or until the newline is found.

1
2
3
4
5
6
7
8
9
    string skype;
    string phone;
	
    cout << "Please input skype: " << endl;
    cin >> skype;
    cin.ignore(1000, '\n');
	
    cout << "Please input phone: " << endl;
    getline (cin, phone);  



http://www.cplusplus.com/reference/istream/istream/ignore/

Note cin.sync() may or may not be useful, depends on the particular compiler implementation.
http://www.cplusplus.com/forum/general/63860/#msg345477


Last edited on
Thanks it works. And 1 more question. If I need to inpute some text with move to next line (getline (cin, mail_1) - give me just 1 line) what should I use instead of getline?
Sorry, I don't quite understand what you are asking. Could you give an example of how you want it to work please.
Yeah my english is bad. Oh, nevermind, I just realize it's unimportant ^_^.
Thanks again for advise.
Last edited on
Topic archived. No new replies allowed.