get and put

May 25, 2013 at 11:10pm
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');
}
}
May 25, 2013 at 11:19pm
What are you typing on the keyboard? Do you press the 'enter' key or not?
May 25, 2013 at 11:22pm
Yeah. Aha its taking the newline as a char and then outputting an extra 'a'?
May 25, 2013 at 11:27pm
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 May 25, 2013 at 11:34pm
Topic archived. No new replies allowed.