#### Archived

This topic is now archived and is closed to further replies.

# Very simple program...Odd results

## Recommended Posts


#include <iostream.h>
#include <conio.h>

void main()
{
char ch;

cout << "Enter letter:";

ch = getch();
cout << ch << endl;

}

When I run this nothing shows up until I hit a key then it prompts you for the letter to enter. If i put an endl in the cout statement it works properly. Not sure why, can anyone explain? Thanks in advance

##### Share on other sites
Because the output is typically fully buffered or perhaps line buffered (line buffered means that the buffer is flushed whenever a newline is placed in the buffer, the buffer is full, or a flush call is made; fully buffered means that the buffer is flushed only when full or told to explicitly flush). That is, it''s kept in a storage area and only written to screen periodically.

If you say cout << flush; you will flush the buffer; if you say cout << endl; you will print a newline and also flush the buffer. This ensures that the message is printed to the screen.

##### Share on other sites
you''re mixing two different input methods, the old c style and a newer c++ style... it can cause problems so it''s best to stick to one or the other.

if you chose to use the c++ streams then you should use the standard ones, not the prestandard ones that you are currently using (they''re 8 years old).

you get to them by including <iostream> rather than iostream.h

it''ll save headaches if you stick with just that.

peace

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982191

• 9
• 24
• 10
• 9
• 13