getline problem

This is a discussion on getline problem within the C++ Programming forums, part of the General Programming Boards category; Is there anyone here that will know why I have to press return twice to get the users input. Using ...

  1. #1
    Steph
    Guest

    Unhappy getline problem

    Is there anyone here that will know why I have to press return twice to get the users input. Using '\n' as third parameter has made know difference. Example code below.

    const int MAX_BUFFER=255;

    int main()
    {
    char Buffer[MAX_BUFFER];

    cout << "Please enter your name: ";
    cin.getline(Name,MAX_BUFFER);

    cout << "Your name is " << Name;

    return 0;
    }

  2. #2
    Unregistered
    Guest
    This works fine for me, hit enter after typing and it processes
    Code:
    #include <iostream.h>
    
    const int MAX_BUFFER = 255; 
    
    int main() 
    { 
      char Buffer[MAX_BUFFER]; 
    
      cout << "Please enter your name: "; 
      cin.getline( Buffer, MAX_BUFFER ); 
    
      cout << "Your name is " << Buffer << endl; 
    
      return 0; 
    }

  3. #3
    Registered User JTtheCPPgod's Avatar
    Join Date
    Dec 2001
    Posts
    44
    I have had a similar problem. Unfortunately I still dunno why it happened. Check out my battleship game on the Game Programming board... When you get on the recent scores (not high scores... shut up, i had like 10 minutes left in class and it was my final due that day and needed something with files) it asks you for your name twice and puts it in the file twice.
    "No! I must have my delicious cupcakes, my sweet cakey treasures, piping hot from their 40 watt WOMB!!!"
    --Captain Murphy

  4. #4
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53

    Re: getline problem

    Originally posted by Steph
    Is there anyone here that will know why I have to press return twice to get the users input. Using '\n' as third parameter has made know difference. Example code below.

    const int MAX_BUFFER=255;

    int main()
    {
    char Buffer[MAX_BUFFER];

    cout << "Please enter your name: ";
    cin.getline(Name,MAX_BUFFER);

    cout << "Your name is " << Name;

    return 0;
    }
    using getline should work fine, but I know that there are some instances where is seems buggy. You can try the following:
    cin.getline(Name,MAX_BUFFER, '\n');//I found that using the delimeter can help. also, you might want to try the get function. The get function is similar but it leaves the delimeter in the buffer, where get lines extracts it from the buffer. If you are using get, you might need to chase it with an ignore function.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    stephs code is wrong.
    unregistereds code is correct.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    Try flushing the stdout buffer with a << endl; in your output.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Ive had that happen to me before (thats one of the reasons i dont use any of the iostream classes anymore). I isolated the problem, but alas, its been so long I have forgotten. I think it has something to do with mixing calls between cin and getline. I think i cleared up the problem by sticking to one or the other, or preceeding the getline call with a call to ignore.

  8. #8
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    sorry, using borland 5 proffesional I had to press enter twice, and i tried all the answers you were given
    Such is life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  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