getline problem

This is a discussion on getline problem within the C++ Programming forums, part of the General Programming Boards category; here is the code: cin.getline (a, 3); cin>>b; cin.getline (c, 3); cin>>d; and here is the problem: it wont prompt ...

  1. #1
    Unregistered
    Guest

    getline problem

    here is the code:

    cin.getline (a, 3);
    cin>>b;
    cin.getline (c, 3);
    cin>>d;


    and here is the problem:

    it wont prompt for c. the variables are all initialized, and everything else that needs to be done is done, unless i missed something. it prompts for a, b, and then d, skipping c. how can i fix it?

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Try cin.ignore(). Chances are that somewhere along the line you are accumulating enough lost \n characters that the program skips an input line where it shouldn't
    Code:
    cin.getline (a, 3); 
    cin.ignore();
    cin>>b; 
    cin.getline (c, 3); 
    cin.ignore();
    cin>>d;
    pointer = NULL

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    *pointer's advise should work. In case your are interested in the details, and to help me keep them straight, I offer the following.

    Istreams offer a number of methods (member functions) to deal with input from the keyboard/files/other sources. Among them are >>, get(), and getline(). Each of these methoods have default behaviour, although get() and getline() allow user to override the defaults. Before describing the behaviours however it is useful to think about how input is achieved.

    Input from the keyboard enters the system as a discrete series of electrical events initiated by pushing a key and file input can be viewed as discrete elelctical events initiated by "reading" from an electronic storage device. Either way, each event represents a value of some sort (letter, digit, punctuation mark, etc.) and each value is stored in an input storage area (buffer) until some event "empties" the buffer. You can manipulate this buffer directly if you want to, although the istream methods are available to make this easier for you.

    For >> the default is to store all input in the buffer until an enter key (or equivalent signal) is encountered. When the enter key signal has been found >> starts to load any information in the buffer into the variable indicated on the right hand side of the >>. >> removes and ignores any leading white space in the buffer, and extracts all data in the buffer up to the first white space after the first non-white space information has been encountered. It leaves the terminating white space and any other remaining data in the buffer.

    The get() method is similar to >> except it is terminated on a given char value, which is the newline char (put in the buffer by pushing the enter key or using the newline char symbol overtly) by default. It leaves the terminating char in the input buffer just like >>. However, you could terminate input on an 'r' or a '*' or a '~' or a ' ' or whatever you want, rather than just whitespace stuff like >>. There are actually several different forms of get(), each slightly different from the other, but they all leave the terminating char in the input buffer.

    The getline() method takes all data out of the input buffer up to a specified amount of data or until a terminating char has been encountered. The terminating char defaults to the newline char but can be overridden by the programmer to be any valid char. If extraction from the input buffer is terminated because of size considerations all the stuff in the input buffer after the preset amount has been removed remains in the buffer. If extraction from the input buffer is terminated because the terminating char was encountered, then the terminating char is removed from the buffer, but any other items after it remain in the buffer.

    How does this relate to your program. Your getline() calls use the default newline char as the terminating char. Therefore it is removed from the buffer after the first call. Even if it weren't removed the first >> would ignore it. However, >> will leave the newline char in the input buffer when it is encountered. Now when the second call to getline() starts the first thing it sees in the newline char and it never extracts anything meaningful to put in variable c. The second getline() dutifully removes the newline char from the input buffer, but even if it didn't, the second >> can proceed because it ignores all preceding whitespace.

    How do you overcome this "defect". The ignore() method of istreams is the key, as *pointer indicated. ignore() reoves up to a given amount of information from the input buffer or all information until a given terminating char is found and ignores it. The terminating char is removed from the buffer and ignored, too, if that is what stops the ignore() process. The default amount of information for ignore() to ignore is one item and the default terminating char is the newline char, but you can ignore as many as you want, and you can terminate on any char you want, too.

    You should be watchful of this type of problem any time you mix istream methods to obtain the input you want. Once you are aware of the possibility that there is a problem it is relatively easy to correct, if you know the default behaviours.

  4. #4
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Well explained guest. Though I would also like to add that before using the istream methods, it would be a good idea to read up on them in either a reference manual or the compiler's help files. This way you know what parameters they can take and you can use them to their fullest advantage.

    I find myself using cin and getline() together in my programs because cin can't take strings separated by whitespace. It's because of this that I recommended cin.ignore() because getline() has its own ignoring function as follows
    getline( a, 80, '\n' );
    This reads a string of up to 80 characters regardless of what type, white space is okay, and when it encounters a newline, it ignores the newline and saves the string to the buffer a.
    pointer = NULL

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    Cant you do something like this to?

    cin.getline(a, 3);
    cin >> b;
    cin.get(); //takes care of anthing in the buffer
    cin.get(c).get(); //reads to c and takes care of the leftovers in the buffer
    cin.getline(d, 3);

    or am I wrong???

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

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