endl and flushing the buffer

前端 未结 5 648
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 14:55

In the C++ primer book, in chapter (1), it mentions the following:

endl is a special value, called a manipulator, that when written t

5条回答
  •  我在风中等你
    2020-11-27 15:16

    One simple code to show you the effects of buffered I/O in c++

    Whatever input you provide is buffered and then passed on to the program variables in case of inputs.

    Have a look at the code below:

    //program to test how buffered  I/O can have unintended effects on our program
    
    #include
    using namespace std;
    
    int main()
    {
        int a;
        char c;
        cin>>a;
        cin>>c;
        cout<<"the number is : "<

    here we have declared two variables one int and one char if we input the number as "12d34" this will cause the int variable to accept only 12 as value and it will discard the rest which will still be there in the buffer. And in the next input the char variable will automatically accept the value "d" without even asking you for any input

提交回复
热议问题