Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

the method 'get' (C++ Programming)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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);

Share this post


Link to post
Share on other sites
Quote:
Original post by Toadhead
yes, but why


cin.get(a).get()



??????
Quote:
Original post by RabidCow
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.
The second get() is used to eat the newline character at the end.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ra
Quote:
Original post by Toadhead
yes, but why


cin.get(a).get()



??????
Quote:
Original post by RabidCow
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.
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!