To overcome the trouble with cin being word oriented, with that I mean it reads single words from input and if words consist of more then one word, like New York, cin buffers York, and use New to store in for example your char array variable.
Luckily the developers of the library add some inteligent functions to the cin object. Created from the class isstream.
cin.getline(), and cin.get(), both read a line of input at a time until they find the '\n' character, but cin.getline() then discards the newline character, whereas cin.get() leaves it in the input queue.
cin.getline() takes two arguments. The first argument is the name of the array destined to hold the line of input, and the second argument is a limit on the number of characters to be read. If this limit is, say, 20, the function reads no more then 19 characters, leaving room to automaticaly add the null character at the end. The getline() member function stops reading input when it reaches this numeric limit or when it reads the newline character, whichever comes first.
example:
cin.getline(name,20);
This reads the entire line into name array, provided that the line consists of 19 or fewer characters.
Even cin.getline() does something very nice, when it reads the line in the character array variable, as it discovers the newline, it removes it from the stream, and replaces it with the null byte and puts that for you at the end of your line in the array.
That is a time saver isn't it!
Another saver and nice future is concatenate member functions!
Since cin.get() and cin.getline() returns the cin object, which is then used as the object that invokes the get() or getline() function.
Example:
cin.getline(name1, ArSize).getline(name2, ArSize);
reads two consecutive input lines into arrays name1 and name2;
it's equivalent to making two separate calls to cin.getline()
Now you wonder why those two functions are almost the same, and which one to use when reading a line of input. The answer is simple, you should use cin.get() most of the times if you want to check if the array was not full.
cin.get() will result after a successful read in a newline char, if it is not newline '\n' then you know the size of the line was to big, and there are still characters waiting to be processed!
Further more, if cin.get() reads an empty line it sets a fail bit, so you can check on that. If the failbit is set, further input is blocked. Also it is possible to restore input after this happens with cin.clear();
If the input string is longer than the allocated space, both getline() and get(), leave the remaining characters in the input queue. However getline() additionally sets the failbit and turns off further input.
IMPORTANT NOTE!
int year;
(cin >> year).get(); // .get() to remove '\n' from input queue
// because cin does read the integer value
// but leave the '\n' untouched!
you have to use .get() in the above example, because if you don't the '\n' character stays in the input queue, and if for example you use getline() to get the address, it will see it as an empty line, it sets the failbit and stores zero characters in the character array!
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten