Confusing IO problem.

Hi everyone,
I'm an absolute newbie in C++, please help me to solve this problem. This problem is something related to the getline method.
I write a program to ask for user's age and name. Just for demonstrating some IO operations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*IO Example*/
#include <iostream>
#include <string>

using namespace std;

int main() {
	int age;
	string name;
	//
	cout << "Please enter your age:";
	cin >> age;
	cout << "Your age is: " << age;
	cout << " so your year of birth is " << 2010 - age << ".\n";
	//
	cout << "Please enter your name: \n";
	getline (cin , name);
	cout << "Hello " << name << " ! What a good day!";
	
	return 0;
}


The program cannot get the name. In fact, it doesn't give me opportunity to type anything.

However, the same code I get from this website works properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> 
#include <string> 
using namespace std; 
 
int main () 
{ 
  string mystr; 
  cout << "What's your name? "; 
  getline (cin, mystr); 
  cout << "Hello " << mystr << ".\n"; 
  cout << "What is your favorite team? "; 
  getline (cin, mystr); 
  cout << "I like " << mystr << " too!\n"; 
  return 0; 
}


Which problem does my program suffer? And what is the difference between the two snippets of code.

It's so easy with someone but please help me,

Thanks very much!
Nichya
getline/cin mixture should be avoided.
When you go with cin, you type in your answer and press enter. Cin takes the typed part and packages it to the integer, leaving the newline in the buffer. When you get to the getline, getline accepts but discards newlines (they are the delimiter character) so you immediately skip the input.
Solution: use only getline or only cin.
The prblem is that you cannot mix getline and cin sometimes. You can try changing your program to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int main() {
	int age;
	string name;
	//
	cout << "Please enter your age:";
	getline (cin, age);
	cout << "Your age is: " << age;
	cout << " so your year of birth is " << 2010 - age << ".\n";
	//
	cout << "Please enter your name: \n";
	getline (cin , name);
	cout << "Hello " << name << " ! What a good day!";
	
	return 0;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main() {
	int age;
	string name;
	//
	cout << "Please enter your age:";
	cin >> age;
	cout << "Your age is: " << age;
	cout << " so your year of birth is " << 2010 - age << ".\n";
	//
	cout << "Please enter your name: \n";
                 cin.ignore(256, '\n');
	getline (cin , name);
	cout << "Hello " << name << " ! What a good day!";
	
	return 0;
}
Oh yeah, @OP: you can use cin.ignore to clear out any unnecessary newlines. In general I'd suggest you do that after cin if you plan on using cin frequently. The long-term fix is, of course, not mixing the two.
Thanks for your helps! I'm just new to C++. It's a little bit difficult to handle something that i had familiarity with in other languages. But i'm eager to learn them and this forum is very helpful. Thank again. :)
The extraction operator, >>, skips the newline character and unless the computer captures the newlie character, the computer does not know where one line ends and the other begins. I am new to C++ as well, so my suggestion is to use cin.ignore as suggested by tummychow. This works well if the programmer knows what they are looking for; i.e.
consider the declaration and input:

1
2
3
4
5
int a, b;

// input
25 67 89 43 72
12 78 34



then consider the following statements:

1
2
3
cin>>a;
cin.ignore(100, '\n');
cin>>b;


cin>>a stores 25 in a.
cin.igonre discards all of the remaining numbers in the first line.
cin>>b stires 12 in b.
Topic archived. No new replies allowed.