views:

636

answers:

6

Ok, so I came across this code snippet in my textbook that's supposed to echo every other character a user types in. Now, I understand the every other character part, but I'm having difficulty with the use of cin.get(). I understand why the first cin.get() is there, but why is it also inside the loop? I'm guessing I'm not fully grasping the nature of input streams...

EDIT: It just clicked... I'm an idiot. Thanks for clearing that up.

char next;
int count = 0;
cout << "Enter a line of input:\n";
cin.get(next);

while (next != '\n')
{     
    if ((count%2) == 0)
    cout << next;
    count++;
    cin.get(next);
}

Thanks in advance!

A: 

Streams in C++ , are buffered. Think of them as a line of letters. When you call cin.get(var) the first character in that line is removed and returned to you. So, that's how it works.

An example would help better. When the first cin.get() is executed, let's say you type in :
LISP
and then, cin.get() will return (in the var.) L and then the buffer will look like ISP... the next call will place I in the var. and the buffer will look like SP and so on...

Aviral Dasgupta
A: 

istream& get(char &c) gets a character from the input stream.

so on the first call cin.get(next) you typed: "hello world!" Then future cin.get(next) will fetch h, e, l, l, etc... on every call until the there are no more characters in the input stream and that's when it will block asking the user for more input.

prime_number
A: 

The call to cin.get(next) that comes before the loop is only placing the first character of buffered user input into the variable 'next.'

Once inside the loop, and the character stored in 'next' has been processed (echoed if at an even index, otherwise ignored), cin.get(next) needs to be called again to retrieve the next character.

csj
A: 

Its printing characters present at even positions

char next;
int count = 0;
cout << "Enter a line of input:\n";
cin.get(next);//gets first character (position zero) from input stream

while (next != '\n')//check whether the character is not line feed(End of the line)
{     
    if ((count%2) == 0)//checks if position is even
    cout << next;      //if yes print that letter
    count++;           //increments the count
    cin.get(next);     //gets next character from input stream
}

We require two cin.get(...)

  • before entering the while loop we need to know first character(position zero)
  • inside while loop for getting next character
Xinus
+1  A: 

cin.get in this case does not "grab a line of text" as you seem to believe. cin.get in this case grabs just a single character. With cin.get you read characters the user is typing in, one by one, one after another. This is why you have cin.get in a loop.

AndreyT
A: 

but what is the use of outside cin.get(ch) what does it do how cin.get() works inside loop both behaviours are lookin different so it's making confusing there is a statement in loop to print the character got using cin.get(next) but it will not print it.... it will print all together after pressing enter key ... actually it should display the characters as we type from the keyboard but it is not actually working like that

Hardik Patadia