Public Group

# the method 'get' (C++ Programming)

This topic is 5133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I have a quesiton about the method 'get'. Here, look at this:
#include <iostream>
using namespace std;

int main() {

char text[20];
cin.get(text).get();

cout << "The text you entered was: " << text;

return 0;
}


I mean.. first you declare "number", than you need to enter a value for the integer number with cout.get(number), but why the '.get()' chained after it???? I got this (in a different example) somewere in my C++ book, and they don't explain why they put .get() after it. If anyone could help me it would be realy appreciated, Rob [Edited by - Toadhead on September 2, 2004 9:10:01 AM]

##### Share on other sites
eh, that is not quite right...

Get() reference

##### Share on other sites
You're reading a character (the first call will be to istream& istream::get(char&), since there isn't one that accepts an int) and then consuming a second character, which is probably supposed to be the the newline from pressing enter after the input.

IMO,

cin >> number;

would be a much better way of doing it, since you would read an actual int instead of an ASCII value. As for the trailing .get(), I don't see any point in it at all.

-RC

Edit: One more thing - in your code, it should be cin.get and not cout.get - probably just a typo, but it's not even going to compile as is.

##### Share on other sites
yes I mean cin and I mean a char insetad of int :P
I know that I can better use cin >> ... but I want to know why they used .get() after the cin.get(text) :/

##### Share on other sites
get calls can be chained, just like >>, so just like you can write:
cin >> a >> b;

you can also write
cin.get(a).get(b);

yes, but why

cin.get(a).get()

??????

##### Share on other sites
Quote:
Quote:
 Original post by RabidCowYou're reading a character (the first call will be to istream& istream::get(char&), since there isn't one that accepts an int) and then consuming a second character, which is probably supposed to be the the newline from pressing enter after the input.
The second get() is used to eat the newline character at the end.

##### Share on other sites
Quote:
Original post by Ra
Quote:
Quote:
 Original post by RabidCowYou're reading a character (the first call will be to istream& istream::get(char&), since there isn't one that accepts an int) and then consuming a second character, which is probably supposed to be the the newline from pressing enter after the input.
The second get() is used to eat the newline character at the end.

Yeah, my book saids something like that toobut I didn't realy understand it.

I think I might understand it now, look, when you just give cin.get(a) and you entered something for it, it will automaticly jump to the next line when you pressed enter. When you use cin.get(a).get() and you press on enter after you entered some text for the variable, you wil continue on the same line.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
frob
11

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×