Advertisement

Newbie cout / kbhit problem

Started by December 01, 2001 06:52 PM
8 comments, last by Peon 23 years ago
I''ve never encountered something like this before; it has me baffled I''m trying to write a small strategy game (loosely based on Adavance Wars for GBA) using just text graphics to test out some of my ideas before I try and design a DirectX version. As luck would have it, however, I encountered one of the strangest errors I''ve ever seen by just making the menu. Here is the complete code (using MSVC++ 6.0, btw):
  

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

//GLOBALS

struct Unit
{
	
	int  UnitId;    //numerical world ID of unit

	int  TypeID;    //type of unit ID

	char name[20];  //name of unit

	int  hp;	//hp (max: 10)

	int  ammo; 	//amount of ammo left

	int  range;     //amount of squares a unit can fire

	int  move;	//amount of squares a unit can move

	int  los;	//amount of squares a unit can see

	int  XCoord;	//x coordinate

	int  YCoord;	//y coordinate

	bool selected;  //determines if an object is selected


};

//FUNCTION PROTOTYPES

int SelectCommander();
int GetInput();

int main()
{
	SelectCommander();

	return(1);

}


int SelectCommander()
{
	cout<<"Player 1 -- Select a Commander: \n\n";
	
	cout<<"Blah1:   BONUSES: - Infantry class units deploy cost -20%\n";
	cout<<"                     - Infantry class units +15% damage\n\n";

	
	cout<<"Blah2:    BONUSES: - APC move +2 squares\n";
	cout<<"                     - All units move +1 square\n";
	cout<<"                     - All units 10% cheaper\n\n";

	cout<<"Blah3:     BONUSES: - Tank class units +30% strength\n";
	cout<<"                     - Missile class units cost 15% more\n";
	cout<<"                     - Missile class units -10% damage\n\n";

	cout<<"Blah4	    BONUSES: - Bio soldier damage +2\n";
	cout<<"                     - Cannot build Tank class units\n";
	cout<<"                     - Bio soldier cost +20%\n\n";

	cout<<"Blah5 	    BONUSES: - All units +2 LOS\n";
	cout<<"                     - Radar range +2 squares\n";
	cout<<"                     - Missile unit range +1 square\n\n";

	
	switch(GetInput())
	{
		case 106: //j (Blah1)

		{
			cout<<"-- Blah1--";

		} break;

		default: break;

	}//end switch



	return(1);

}

int GetInput()
{
	int choice = 0;

	while(!(kbhit()))
	{
		if (_kbhit())
		{
			choice = getch();

		}//endif


	}//end while


	return(choice);

}//end GetInput()


  
Okay, so if you run this program, it should logically print out the complete list of descriptions and then wait for input, right? Not so. Here is the output: * CUT TO SAVE SPACE Blah4 BONUSES: - Bio soldier damage +2 j - Cannot build Tank class units - Bio soldier cost +20% Blah5 BONUSES: - All units +2 LOS - Radar range +2 squares - Missile unit range +1 square j Press any key to continue Shouldn''t all the cout<< statements logically execute before the input loop? Right now, it prints up to the "BONUSES: - Bio soldier damage +2", then stops and waits for input, and then prints the rest of the list. I can''t seem to figure out _why_ it would just stop cout<<-ing in the middle of the descriptions and continue after the input. It just doesn''t make sense (to me) Thanks for any help
Peon
It''s buffering the output. Use "cout.flush()" before you ask for input.

[Resist Windows XP''s Invasive Production Activation Technology!]
Advertisement
Ah okay, never even heard of that command before. What makes it require cout.flush()? Is it the amount of info, or something else?
Peon
and this is why i stick to using printf, heh. basically internally the class buffers a certain amount and prints it when the buffer is full or there is time left to just print it. this is why printf is slower then using cout, since printf waits till the buffer is flushed and all output has been sent to the console.
quote: Original post by Anonymous Poster
this is why printf is slower then using cout, since printf waits till the buffer is flushed and all output has been sent to the console.

#1. Printf is actually faster in most (not all, of course) implementations of the C/C++ standard libraries.
#2. Printf doesn't wait until it's flushed (at least not in my experience). I still have to called fflush(stdout) (for example) when debugging incase the application crashes before the buffer is emptied.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on December 2, 2001 6:58:41 PM
hey ya thanks for the tip... I never even heard of cout.flush b4!

RoB~
Advertisement
hey ya thanks for the tip... I never even heard of cout.flush b4!

RoB~
cout << endl outputs a newline and flushes the buffer as well. Use ''\n'' in interim newlines and endl at points where you want the entire buffer flushed.
Hmm... I''d never used cout.flush either. Of course, I usually did use \n or endl so maybe that''s why.
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
Ah, thanks for the tip. I''ve always though \n and endl were exactly the same. That might explain why I never had to use flush() before (I used to use endl a lot, now I prefer \n)
Peon

This topic is closed to new replies.

Advertisement