Sign in to follow this  
Toadhead

the method 'get' (C++ Programming)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this