cin.getline : strange error

This is a discussion on cin.getline : strange error within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. What I am simply trying to do is to read two names that can contain spaces. Here is ...

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    cin.getline : strange error

    Hello everyone. What I am simply trying to do is to read two names that can contain spaces.
    Here is my code:
    Code:
    void Foo :: Cinema
    {
    	....
    	....
    
    	char title[20], director[20];
    
    	cout << "Give me the title :" << endl;			
    	cin.getline(title, 20, '\n');
    
    	cout << "Give me the director : " << endl;
    	cin.getline(director,20, '\n');
    
         ....
    }
    I tested the same code inside main and it works normally but now, in this function something strange occurs. What I see when run the program is
    Code:
    Give me the title : 
    Give me the director : (cursor here)
    and as a result I can put only the name of the director.
    What could be wrong? How can I fix this?

    p.s. I use Visual Studio 2005

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Are you cin-ning before the first getline()?
    Try cin.ignore() before the first cout, cin.getline pair.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Also, you should look into std::string rather than character arrays.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    A hypothetical bit of code:
    Code:
    int foo;
    cout << "Enter an int: ";
    cin >> foo;
    string bar;
    cout << "Enter a string: ";
    getline(cin,bar);
    What happens in the above bit of code is that the cin >> foo line above leaves the newline in the stream (the user enters a number then presses the enter key which inserts a newline into the end of the stream). The number is extracted and converted and stored in foo but the newline causes problem later on. When we reach the getline, the newline character is the first thing the getline sees and it thinks the user entered something and pressed the enter key. This means you see the prompt to "Enter a string: " but are unable to actually enter a string because the getline call has already processed the newline. So... anytime you mix cin >> and getline calls, you need to call the stream's (cin in this case) ignore member function after the cin >> and prior to the getline call.
    "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

  5. #5
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Quote Originally Posted by twomers View Post
    Are you cin-ning before the first getline()?
    Try cin.ignore() before the first cout, cin.getline pair.
    You're so right. I used cin.ignore() and the problem is solved.
    Before you told me this I used cin.ignore(1,'\n') and didn't work. What does cin.ignore() do differently ?

    @ hk_mp5kpdw : thanks for the analytic explanation. I must say I had tried to use cout << flush;
    (without knowing what exactly does!) in order to clear the newlines or whatever, and it didn't fix the problem.
    Quote Originally Posted by hk_mp3kpdw
    (the user enters a number then presses the enter key which inserts a newline into the end of the stream)
    you probably mean input stream , right ?
    Last edited by kantze; 09-08-2007 at 03:38 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> What does cin.ignore() do differently ?
    cin.ignore(1,'\n') and cin.ignore() should behave in exactly the same way as far as I can tell. If you can provide an example where they don't please post it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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