Thread: problem with getline

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    Talking problem with getline

    Code:
    void set_characterNameAgeSex()
    {
    	char setName[ 50 ];
    	char *sTransfer;
    	short setSex;
    	short setAge;
    
    	// set name
    	cout << "Enter a name for your character(50 char. max): ";
    	cin.getline( setName, 49 );
    
    	sTransfer = setName;
    	sName[ 0 ] = sTransfer;
    The problem I'm having is that cin.getline is being skipped. It skips user input and goes to the next line of code.

    I have no idea what is wrong with getline. If I do this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    char *hstring[ 1 ];
    
    void whatever();
    
    int main()
    {
    	char *s1;
    	char name[ 50 ];
    
    	cout << "Enter a string: ";
    	cin.getline( name, 49 );
    
    	s1 = name;
    	hstring[ 0 ] = s1;
    
    	cout << "\n" << hstring[ 0 ] << endl;
    
    	return 0;
    }
    It works fine. User input works and hstring[0] outputs the entered string.

    What to do? I'm clueless!

    One thing I noticed: In the first example, (I'm using MSVC++6.0 btw) if I put my mouse over sName[ 0 ], a tooltip pops up showing "char sName[ 50 ]" but if I put my mouse over hstring[0] in the first example, the tooltip reads, "char *hstring[0]".

    Why does it show two different things when both the code is exactly the same except with different named variables?

    Thanks in advance for replies

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Try cin.ignore() before you cin.getline()
    eg:

    Code:
    char setName[ 50 ];
    	char *sTransfer;
    	short setSex;
    	short setAge;
    
    	// set name
    	cout << "Enter a name for your character(50 char. max): ";
            cin.ignore(80, '\n');//Should Solve The Problem
    	cin.getline( setName, 49 );
    
    	sTransfer = setName;
    	sName[ 0 ] = sTransfer;
    Last edited by prog-bman; 06-05-2004 at 12:49 AM.
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you get input from the user prior to this? Something like:
    cin >> something;

    If so, you probably have a newline left in the input buffer, which you need to remove. Try something like:
    Code:
    	cout << "Enter a name for your character(50 char. max): ";
    	cin.ignore(std::numeric_limits < streamsize >::max(), '\n');
    	cin.getline( setName, 50 );
    Or simpler:
    Code:
    	cout << "Enter a name for your character(50 char. max): ";
    	cin.ignore(80, '\n');
    	cin.getline( setName, 50 );
    And it would probably be better to put the ignore() where you received input from the user previously.
    cin >> something;
    cin.ignore(std::numeric_limits < streamsize >::max(), '\n');

    Also, your getline should probably match the size of your array, so:
    > cin.getline( name, 49 );
    would instead be:
    cin.getline( name, 50 );
    Last edited by swoopy; 06-05-2004 at 12:58 AM.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    Yeah, I have the user enter input before that line. And cin.ignore works, but I was wondering what you mean by saying I have a /newline in the input buffer.

    Thanks.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I was wondering what you mean by saying I have a /newline in the input buffer
    If you did something like this:
    cin >>mynumber;
    to get a number from the user, they would have had to type something like this on the command line:
    1234<enter>
    The <enter> would be a newline character, and wouldn't be read into your program by the cin function, as you only asked for a number. Using cin.ignore() clears all characters from the input buffer, up to and including the newline character, or up to the length specified.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM