Start New Discussion within our Software Development Community

i have just observed this wierd thing with visual c++
the following is my code.[please forgive my poor C++ standard. im using it only because im doing it 4 a school project and the examiner wont be bright enough to understand what iss not in the syllabus. this is sad but true state of affairs in india)

#include <iostream.h>
#include <string.h>

int main()
{
    char c[10];
    strcpy(c,"hello\n");
    cout<<c;
    cout<<"world";
    cin.get(c[0]);
    return 0;
}

the statement cout<<c; and cout<<"world;" doesnt really output just after it has been executed.instead all the action that is taken by these too lines of code is the buffering. I observe that the output it gets done only when cin.get(...) is being executed dunno why. seems very wierd to me. i think this is more due to lack of my understanding of working of the iostreams than an issue of VC++.

id be grateful if someone clarified the working of istream and ostream(in this case or in general)

and please tell me a way to empty(forcefully output) a buffer at will.

//note that i have made the above observations using VC++ debugger

>I observe that the output it gets done only when cin.get(...) is being executed dunno why.
Because cout is tied to cin. When you make an input request on cin, cout is automatically flushed.

>please tell me a way to empty(forcefully output) a buffer at will.
There are a number of ways, but the two most common are:

cout<<endl; // Print a newline and flush
cout<<flush; // Just flush
This question has already been answered. Start a new discussion instead.