getline problem.

This is a discussion on getline problem. within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if anyone could spot why my program 'misses out' my first question. The program compiles, but ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    getline problem.

    Hi,

    I was wondering if anyone could spot why my program 'misses out' my first question.

    The program compiles, but when I run it, it immediately asks:

    Name?
    Address?
    It isn't giving me chance to enter a name,.

    It instead jumps straight to the Address question.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    
    	do{
    		int choice = 0;
    		cout << "Choose one of the options below:" << endl;
    		cout << "1) Enter a customer" << endl;
    		cout << "2) Exit the program" << endl;
    		cin >> choice;
    
    		switch(choice)
    		{
    		case 1:
    			{
    				cout << "You chose to enter a lead" << endl;
    				//---------------------
    				//Collect the details
    				//---------------------
    				string mainname;
    				cout << "Name? ";
    				getline (cin, mainname);
    				cout << "\n";
    
    				string mainaddress;
    				cout << "Address? ";
    				getline (cin, mainaddress);
    				cout << "\n";
    
    				string mainpostcode;
    				cout << "Post Code? ";
    				getline (cin, mainpostcode);
    				cout << "\n";
    
    				string maintelno;
    				cout << "Telephone Number? ";
    				getline (cin, maintelno);
    				cout << "\n";
    
    				string maindetails;
    				cout << "Details? ";
    				getline (cin, maindetails);
    				cout << "\n";
    
    				break;
    			}
    
    		case 2:
    			{
    				cout << "You chose to exit the program" << endl;
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system ("PAUSE");
    
    	return 0;
    }
    Thanks for any help, I really appreciate it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    You will need to flush the input buffer. I suggest a search on the problem. You will find lots of answers to a common problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    do{
        int choice = 0;
        cout << "Choose one of the options below:" << endl;
        cout << "1) Enter a customer" << endl;
        cout << "2) Exit the program" << endl;
        cin >> choice;
    
        switch(choice)
        {
        case 1:
            {
                cout << "You chose to enter a lead" << endl;
                //---------------------
                //Collect the details
                //---------------------
                string mainname;
                cout << "Name? ";
                getline (cin, mainname);
    The problem alluded to by Elysia is in that first cin >> call. After you've entered 1 or 2 and pressed the "Enter" key, the 1 or 2 will be stored in your variable choice. The "Enter" key however is not extracted and remains in the input stream. By the time you get to the getline function, that newline is still in the input stream and is the first thing that gets extracted by the getline call. Since getline sees a newline it thinks that the user (you) entered something and moves on in the code to the next getline call (to get the address) and does not appear to let you enter anything for the name.

    Mixing getline and straight cin >> statements in ones code is often problematic with beginners unless they realize what's happening and how to deal with it. A call to cin's ignore member function after every use of the stream extraction operator (>>) (before the call to the next getline) is usually all that it takes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thank you very much for explaining it so well to me.

    Here is the working code incase it helps anyone in the future:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    
    	do{
    		int choice = 0;
    		cout << "Choose one of the options below:" << endl;
    		cout << "1) Enter a  customer" << endl;
    		cout << "2) Exit the program" << endl;
    		cin >> choice;
    		cin.ignore();
    
    		switch(choice)
    		{
    		case 1:
    			{
    				cout << "You chose to enter a lead" << endl;
    				//---------------------
    				//Collect the customer's details
    				//---------------------
    				string mainname;
    				cout << "Name? " << endl;
    				getline (cin, mainname);
    
    				string mainaddress;
    				cout << "Address? " << endl;
    				getline (cin, mainaddress);
    
    				string mainpostcode;
    				cout << "Post Code? " << endl;
    				getline (cin, mainpostcode);
    
    				string maintelno;
    				cout << "Telephone Number? " << endl;
    				getline (cin, maintelno);
    
    				string maindetails;
    				cout << "Details? " << endl;
    				getline (cin, maindetails);
    
    				cout << "BreakPoint" << endl;
    				break;
    			}
    
    		case 2:
    			{
    				cout << "You chose to exit the program" << endl;
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system ("PAUSE");
    
    	return 0;
    }
    Again, thanks very much !
    Last edited by Swerve; 10-05-2009 at 11:27 AM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Code:
    		case 2:
    			{
    				cout << "You chose to exit the program" << endl;
    				break;
    			}
    		}
    You may want to modify case 2 so that it actually exits the program instead of just printing out a line of text
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    One of these days, we really do have a create an article about this phenomenon, since it is SO common.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 11:35 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 02:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 12:59 PM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM

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