getline trouble

This is a discussion on getline trouble within the C++ Programming forums, part of the General Programming Boards category; This must be something simple, but its driving me mad!! Why does the code shown below not extract the string ...

  1. #1
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    getline trouble

    This must be something simple, but its driving me mad!! Why does the code shown below not extract the string for the school? All the other bits wrok fine, except the
    Code:
    cin.getline( school, MAX, '\n' );
    bit doesn't seem to execute
    Code:
    	const int MAX = 30;
    
    	system( "cls" );
    	char first[ MAX ];
    	cout << "\nEnter first name : ";
    	cin.getline( first, MAX, '\n' );
    
    	char last[ MAX ];
    	cout << "\nEnter last name : ";
    	cin.getline( last, MAX, '\n' );
    
    	int age;
    	cout << "\nEnter age : ";
    	cin >> age;
    	if( cin.fail( ) )
    	{
    		cerr << "Bad input fool!" << endl;
    		return false;
    	}
    	cin.ignore( 80, '\n' );
    
    	char sex;
    	cout << "\nSex ( M or F ): ";
    	cin >> sex;
    
    	if( sex != 'm' && sex != 'M' && sex != 'f' && sex != 'F'  )
    	{
    		cerr << "Bad input fool!" << endl;
    		return false;
    	}
    
    	char school[ MAX ];
    	cout << "\nEnter name of school : ";
    	cin.getline( school, MAX, '\n' );        //this bit aint working!
    
    	char grade;
    	cout << "\nEnter competitors grade :";
    	cin >> grade;
    
    	int heightOrWeight;
    
    	cout << ( age < 18 ? "\nEnter height ( cm ): " : "\nEnter weight ( kg ) : " );
    	cin >> heightOrWeight;
    Couldn't think of anything interesting, cool or funny - sorry.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    You forgot one of these

    cin.ignore( 80, '\n' );

    Which you need before each cin.getline() to tidy up after cin >> var; calls.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Should they come after all input then? I've only ever used ignore after reading integer values.
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Salem's right.

    Different data types may be handled by different buffers, but the fact is that you need to flush the rascal to be sure.

    In your case, you're inputting char data in back-to-back operations. The '\n' character may very well not be removed from the stream when you input the school name.

    getline() reads to, and consumes '\n' (as opposed to get() which reads to, but leaves, '\n').

    Fair guess? getline() reads and "eats" the '\n' character still in the stream, and then leaves the pointer at the start of the school name sitting and waiting for somewhere to go.

    In other words, the school name is there, but still stuck in the stream buffer. Follow Salem's advice.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Unregistered
    Guest
    always use ignore() before calls to get() or getline() unless you are absolutely sure what's already in the istream buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little trouble with getline
    By McFury in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 01:27 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21