Archived

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

burgc002

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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites