get and put

Why does the following result in the char entered followed by an 'a' and then a consecutive 'a' on a newline. Should it not just give one 'a' after the entered char on a newline?

1
2
3
4
5
6
7
 int main ( )
{  char ch;
   while (cin.get(ch) && ch != 'q'){
      cout.put(ch);
      cout.put('a');
}
}
What are you typing on the keyboard? Do you press the 'enter' key or not?
Yeah. Aha its taking the newline as a char and then outputting an extra 'a'?
it works with this:

1
2
3
4
5
6
7
8
int main ( )
{  char ch;
   while (cin.get(ch) && ch != 'q'){
      cin.ignore(1);
	cout.put(ch);
      cout.put('a');
}
}


But i don't understand. Surely it should ignore the char and then put the newline instead of the other way around?

Because ignore is used to ignore the oncoming input stream. So it takes the first char, outputs an a and then ignores the newline.
Last edited on
Topic archived. No new replies allowed.