2

The code below shows the output as

enter something
abc
you entered: a
you entered: b
you entered: c
  #include "stdafx.h"
    #include <iostream>

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    char c;

    while(cin>>c)
    {
    //Do something  
        cout<<"you entered: "<<c<<"\n";
    }


    return 0;
    }

why is it not showing only the first character entered? I know I can force it to ignore the cin buffer after first char by using

cin.ignore(1,'\n')

but shouldnt it only ready one character and ignore the rest?

5
  • Your example says that it is showing the first character entered. Are you asking how to get it to ignore any characters entered after the first character? Jan 21, 2013 at 12:54
  • No, currently its showing all chars entered and I am curious to know why.
    – swellcode
    Jan 21, 2013 at 13:05
  • The std::cin stream is buffered. This means when you try and read stuff it will block until the either the buffer is full or flushed. By default the std::cin buffer is flushed when you hit <enter>. But your code is behaving correctly. It loops until the std::cin stream is empty (hit <ctrl>-D on unix or <ctrl>-Z on Win) to simulate an end of stream. Jan 21, 2013 at 16:47
  • @LokiAstari, I agree with your answer but I think your 2nd+3rd & 5th sentence are contradictory. i.e. I do hit enter but it does not exhibit the default behavior of flushing the buffer.
    – swellcode
    Jan 22, 2013 at 1:39
  • @swellcode: If it did not flush the buffer when you hit enter then std::cin >> c would block until the buffer is full (about 2K worth of characters) so I am pretty sure it did flush. I see your input abc<enter> in the example usage. Jan 22, 2013 at 3:06

3 Answers 3

1

... but shouldnt it only ready one character and ignore the rest?

No. It works correctly and I do not know where you got the idea that it should ignore something. It reads whatever is there in the buffer, and blocks waiting for more input iff the buffer is empty. In your case it gets empty only after three iterations of the while loop.

1
  • I assumed it flushed the buffer after assigning cin to char in every iteration of the loop. Thanks for the clarification.
    – swellcode
    Jan 21, 2013 at 19:24
0

Your code says while (cin >> c) doStuff. That is: as long as there are characters to be read, doStuff. So the program is doing exactly that. Did you perhaps mean if (cin >> c)?

2
  • True, but it should prompt me for input every time it iterates the while loop, correct? In this case, its not. It just takes the buffered chars and outputs them. Once the buffered chars are output to screen, it then prompts for input. I think if(cin>>c) would result in the same behavior.
    – swellcode
    Jan 21, 2013 at 13:01
  • Correction- I thought Angew meant using the if condition inside the while loop.. The requirement is to keep an infinite loop to keep reading input characters and ignoring everything after the first character input.
    – swellcode
    Jan 21, 2013 at 13:15
0

cin can only process the input from the keyboard once the RETURN key has been pressed. The while loop continued the operation till all the characters in the cin buffer gets printed one by one.

So is the program behaves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.