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.

/*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 allow me to type.

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

Thanks very much!
Nichya

>> The program cannot get the name. In fact, it doesn't allow me to type

That because when you read the age, the stream reads the number and
the newline character. That new line character is being read into the
variable name. To fix that problem add this :

/*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";
     {
        string dummy;
        getline(cin,dummy);
      }
	cout << "Please enter your name: \n";
	getline (cin , name);
	cout << "Hello " << name << " ! What a good day!";
	
	return 0;
}

Edited 6 Years Ago by firstPerson: n/a

Thank for your help. It works with me now. I'm still confused a bit.

May I ask you one more question. That is about this snippet of code:

#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; 
}

Why it work properly without using your solution? What is the difference between them.

Thank again!

>> Why it work properly without using your solution? What is the difference between them.

Because before you used cin >> ... , the cin ignores spaces and newline,
and in your program before, the newline character is still left in your
stream and when you try to cin something it automatically read the newline.

But if you use getline(...), then getline reads the input plus the newline character( when you press enter) so in the above code, there is no newline character left in the stream.

Edited 6 Years Ago by firstPerson: n/a

In the first example with cin >> age; Say you type in 20 and Enter. 20 gets read into your int and the newline ('\n') from the enter remains in the input stream.
getline is designed to read until a newline occurs (which it reads and discards), so as it comes along and reads the stream it hits \n right away and quits thinking its got what it was looking for, so your code effectively skips down to your next newline.

What firstPerson has prescribed is putting in a getline to sop up that extra newline so the stream is fresh for the subsequent real getline. A call to cin.ignore(); can be used for the same purpose as his added getline.

So in the second piece of code you've taken up the first string (including newline) but disposed of \n before the next call to getline.

EDIT: firstPerson beat me to it :)

EDIT II: You probably don't even need to deal with the getline and just cin >> yourstring;

Edited 6 Years Ago by jonsca: n/a

Thank for your helps. I understand this problem now.

Nice day :)
Nichya

This article has been dead for over six months. Start a new discussion instead.