Thread: Please help me to solve this IO problem.

  1. #1
    Registered User
    Join Date
    Jan 2010

    Wink Please help me to solve this 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.

    /*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.

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

    Thanks very much!

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    This is because your input buffer looks like this:
    4  2  \n J  o  e  B  o  b  \n
    X  X  ^
    At the time you call getline, ^ is where the input is, X has already been read by the >>. getline() sees the \n, and thinks that it has read 1 line, with no characters except for \n.

    You have a few choices:
    1) Call cin.ignore() to skip 1 character (hopefully the \n)
    2) Call getline(), and ignore it, then call it for real
    3) Write something a bit smarter based on 1 or 2
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jan 2005
    A simple solution that will always work with this type of setup based on #1 is to call cin.ignore() immediately after every call to cin >>. (But never immediately after a call to getline.)

    Since operator>> stops at whitespace and leaves it in the stream, there will always be a character to ignore. Since operator>> skips initial whitespace, it won't cause problems if have back to back calls to cin >> with the cin.ignore() in between.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. Problem I can't seem to solve
    By psykik in forum C Programming
    Replies: 15
    Last Post: 05-19-2005, 12:18 PM
  3. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 12:31 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM

Tags for this Thread