std::cin.getline( ) vs. std::cin - c++

When should std::cin.getline() be used? What does it differ from std::cin?

Let's take std::cin.getline() apart. First, there's std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.
std::cin is such an object. It's the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.
Finally, .getline() is one such method of std::cin (and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin here), and where to put those characters. The precise number of characters can vary: .getline() will stop in three cases:
1. The end of a line is reached
2. There are no characters left in the input (doesn't happen normally on std::cin as you can keep typing)
3. The maximum number of characters is read.
There are other methods and functions that can be used with the std::cin object, e.g.
std::string s;
int i;
std::cin >> s; // Read a single word from std::cin
std::cin >> i; // Read a single number from std::cin
std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
std::cin.ignore(100); // Ignore the next 100 characters of std::cin

In case with char*, std::cin.getline getting line, instead of std::cin getting first word.

Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?
Basically, std::cin (or more generally, any std::istream) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;. std::cin.getline() is used simply to fill a raw char * buffer.

(Very simplefied)My answer is, that std :: cin.getline() can contain spaces, while std :: cin >> can not.

As already others have answered (even better) roughly speaking, use getline() to read an entire line (i.e., a string terminating with \n) and cin>>var to read a number compatible with the type of var (integer, float, double etc.) or a single word.
In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:
int a;
string s;
cin>>a;
getline(cin, s)
cin leaves an end of line, \n, character which is then read by getline();. It is possible to overcome this problem by using cin.ignore().
int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)

Related

How store integers given in a line in diferent variables? [duplicate]

I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it.
Can one input multiple things from standard input in one line? I mean this:
float a, b;
char c;
// It is safe to assume a, b, c will be in float, float, char form?
cin >> a >> b >> c;
Yes, you can input multiple items from cin, using exactly the syntax you describe. The result is essentially identical to:
cin >> a;
cin >> b;
cin >> c;
This is due to a technique called "operator chaining".
Each call to operator>>(istream&, T) (where T is some arbitrary type) returns a reference to its first argument. So cin >> a returns cin, which can be used as (cin>>a)>>b and so forth.
Note that each call to operator>>(istream&, T) first consumes all whitespace characters, then as many characters as is required to satisfy the input operation, up to (but not including) the first next whitespace character, invalid character, or EOF.
Yes, you can.
From cplusplus.com:
Because these functions are operator overloading functions, the usual way in which they are called is:
strm >> variable;
Where strm is the identifier of a istream object and variable is an object of any type supported as right parameter. It is also possible to call a succession of extraction operations as:
strm >> variable1 >> variable2 >> variable3; //...
which is the same as performing successive extractions from the same object strm.
Just replace strm with cin.

C++ String parse of white spaces [duplicate]

When should std::cin.getline() be used? What does it differ from std::cin?
Let's take std::cin.getline() apart. First, there's std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.
std::cin is such an object. It's the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.
Finally, .getline() is one such method of std::cin (and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin here), and where to put those characters. The precise number of characters can vary: .getline() will stop in three cases:
1. The end of a line is reached
2. There are no characters left in the input (doesn't happen normally on std::cin as you can keep typing)
3. The maximum number of characters is read.
There are other methods and functions that can be used with the std::cin object, e.g.
std::string s;
int i;
std::cin >> s; // Read a single word from std::cin
std::cin >> i; // Read a single number from std::cin
std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
std::cin.ignore(100); // Ignore the next 100 characters of std::cin
In case with char*, std::cin.getline getting line, instead of std::cin getting first word.
Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?
Basically, std::cin (or more generally, any std::istream) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;. std::cin.getline() is used simply to fill a raw char * buffer.
(Very simplefied)My answer is, that std :: cin.getline() can contain spaces, while std :: cin >> can not.
As already others have answered (even better) roughly speaking, use getline() to read an entire line (i.e., a string terminating with \n) and cin>>var to read a number compatible with the type of var (integer, float, double etc.) or a single word.
In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:
int a;
string s;
cin>>a;
getline(cin, s)
cin leaves an end of line, \n, character which is then read by getline();. It is possible to overcome this problem by using cin.ignore().
int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)

Difference between istream::get(char&) and operator>> (char&)

My question seems to be the same as this one, but I didn't find an answer since the original question seems to ask something more specific.
In C++98, what is the difference between
char c;
cin.get(c);
and
char c;
cin >> c;
?
I've checked the cplusplus reference for get and operator>>, and they look the same to me.
I've tried above code and they seem to behave the same when I input a char.
The difference depends on when there is a whitespace character on the stream buffer.
Consider the input ' foo'
char c;
cin.get(c);
Will store ' ' in c
However
char c;
cin >> c;
Will skip the whitespace and store 'f' in c
In addition to what's already been said, std::istream::get() is also an unformatted input function so the gcount() of the stream is affected, unlike the formatted extractor. Most of the overloads of get() and getline() have mostly been made obselete by the introduction of std::string, its stream extractors, and std::getline(). I'd say to use std::istream::get() whenever you need a single, unformatted character straight from the buffer (by using its single or zero argument overload). It's certainly quicker than turning off the skipping of whitespace first before using the formatted extractor. Also use std::string instead of raw character buffers and is >> str for formatted data or std::getline(is, str) for unformatted data.

cin doesn't take input as ENTER

char ch[4];
char* ptr;
ptr = ch;
while(1)
{
cin >> *ptr;
if(*ptr == '\n')
break;
ptr++;
}
Here I just wrote a bit of sample code where I am trying to get out of a while loop when user writes ENTER but it's not working. Please help me. Thank you in advance.
To get a single character, use std::istream::get. This should work for getting newlines as well.
But instead of getting characters in a loop until you get a newline, why not just use something like std::getline:
std::string str;
std::getline(cin, str);
Or if you only want to get max three characters you can use std::istream::getline:
char ch[4];
cin.getline(ch, 4, '\n');
You are reading input into the value of a character. That's what *ptr means. I think you want just plain ptr, which is a pointer to an array of characters, which is something that is meant to receive data. What you wrote is basically this:
char c;
cin >> c;
I don't think that's what you meant, nor would it work even if it were, since as Joachim Pileborg points out above, the >> operator skips whitespace like newlines. In general, it is always best to be very robust when it comes to reading input. Provide adequate space, and either use a variable that can grow automatically (like std::string) or tell the system how much space you have (like fgets()).
The following will read a line:
istream& getline (char* s, streamsize n );
The extraction operator would skip leading white-spaces and stop execution on encountering any subsequent white-space. So, when you want to do something like this, use std::istream::get() or std::istream::getline().

Multiple inputs on one line

I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it.
Can one input multiple things from standard input in one line? I mean this:
float a, b;
char c;
// It is safe to assume a, b, c will be in float, float, char form?
cin >> a >> b >> c;
Yes, you can input multiple items from cin, using exactly the syntax you describe. The result is essentially identical to:
cin >> a;
cin >> b;
cin >> c;
This is due to a technique called "operator chaining".
Each call to operator>>(istream&, T) (where T is some arbitrary type) returns a reference to its first argument. So cin >> a returns cin, which can be used as (cin>>a)>>b and so forth.
Note that each call to operator>>(istream&, T) first consumes all whitespace characters, then as many characters as is required to satisfy the input operation, up to (but not including) the first next whitespace character, invalid character, or EOF.
Yes, you can.
From cplusplus.com:
Because these functions are operator overloading functions, the usual way in which they are called is:
strm >> variable;
Where strm is the identifier of a istream object and variable is an object of any type supported as right parameter. It is also possible to call a succession of extraction operations as:
strm >> variable1 >> variable2 >> variable3; //...
which is the same as performing successive extractions from the same object strm.
Just replace strm with cin.