here is the code:
cin.getline (a, 3);
cin.getline (c, 3);
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?
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
cin.getline (a, 3);
cin.getline (c, 3);
*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.
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.
Cant you do something like this to?
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
or am I wrong???