Like if you getline(name,cin) and cin>>name
whats the difference? I heard that getline allows you
to input strings with spaces but i tried it with cin
and it still works.
Like if you getline(name,cin) and cin>>name
whats the difference? I heard that getline allows you
to input strings with spaces but i tried it with cin
and it still works.
cin is a premade object of the istream class. It comes with a number of methods (class functions or function shortcuts--aka operators). Two of the class functions are getline() and >>. >> ignores whitespace, except to use them as terminators for input to the variable. getline() doesn't ignore whitespace, unless you use a whitespace char as the terminating char. There are two forms of getline()--one for straight C++ strings and the other for objects of the STL string class. In order to use getline you need to use the correct version for your type of string.
null terminated string:
char first[90];
cin.getline(first, 89, '~');
STL string
string second;
getline(cin, second, '~');
Last edited by elad; 05-22-2003 at 07:44 PM.
what he said in a nutshell...
you can have spaces with getline, but not with cin.
"if you getline(name,cin) and cin>>name
whats the difference?"
First of all, getline(name, cin) will give you an error.
"I heard that getline allows you
to input strings with spaces but i tried it with cin
and it still works."
Sorry, but you are mistaken. The >> operator ingnores any leading whitespace and stops reading as soon as it encounters whitespace. Therefore, if you are trying to read in something like this:
See spot run.
"See" is the only thing that will be read in if you use
string name;
cin>>name;
On the other hand if you use
getline(cin, name);
You will read the whole sentence into name.
Last edited by 7stud; 05-22-2003 at 07:52 PM.
"...not with >>" would be correct. cin is an object.Originally posted by Jamsan
what he said in a nutshell...
you can have spaces with getline, but not with cin.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law