Hai, I'm a higher secondary student in kerala. As our project I have to create a simple car racing program in c++. I implemented the following code fragment. May I get some instructions to improve it?
Especially the method of accessing input to controll movement of object?

void car(int &);
void right(int &);
void left(int &);
void bgmove(int);
void play(void);
void car(int &cp)
{	gotoxy(cp,25);
	cout<<"|    |";
void right(int &cp)
{       if(cp<40)
void left(int &cp)
{       if(cp>20)

void bgmove(int i)
{       for(int l=i;l<50;l+=5)

void main()
	int cp=28,n,s=1;
	char m;
	cout<<"Enter number of laps\n";
	for(int i=0;i<n;i++)
	{	for(int j=1;j<5;j+=s)
	       {        clrscr();
			case '4':left(cp);
			case '6':right(cp);
			case '8':break;
			case 'q':exit(0);
			case 'c':if(s<5)
			default :goto move;

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.

May I get some instructions to improve it?

Yes, don't use iostream.h, use iostream, don't use void main, use int main,
don't use gotoxy, find a replacement, and don't use goto. Also why
do you have a case '8' if you don;t use it?

Thank you for co-operation.
OK I agree. 8 is not required.
How can I move the car if gotoxy is removed?
May I get the graphics codes for a car,road and for tree with explenations?
How can I include sound file in the program?
Can I replace m=getch() with any other function effective than getch()?

Restricting yourself to C++ using standard methods and standard libraries to maintain portability restricts graphics capability. Conversely, doing something with graphics in C++ beyond simple static "ASCII art" restricts portability. To my knowledge at least, you basically have to accept one or the other restrictions if you want to use C++.

However, you don't usually have to use goto() in either standard C++ ASCII art or full blown graphics. Generally speaking goto() can be replaced with loops and control statements.

If your instructor has approved use of non-standard functions/libraries to do this assignment and you are happy with the results you have achieved, then go ahead and stick with what you have.

However, you could think about the field you want to display as table of char. Some of the char are spaces so it looks like nothing is there. Other char may represent the racetrack or the car as it "moves" around the (square) race track. Each move of the car can then represented as a change in the position of the appropriate char representing the car in the table without using goto(). The table can be redisplayed after a given interval of time or after a given event. Likewise, the interface to accept user input to determine direction and speed of the car and how to represent said changes to the position of the "car" within the table is pretty straightforward.

Unfortunately, the graphical appearance of ASCII art isn't very appealing. In a graphics program you are essentially manipulating a table of pixels as opposed to a table of char but the philosophy and sequence of steps to demonstrate the game is similar, once you get beyond the graphics interface itself.

If you want you can replace gotoxy with this windows function :

// Cursor Position //
void gotoxy(short x, short y)
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {x, y};
    SetConsoleCursorPosition(handle, position);
This article has been dead for over six months. Start a new discussion instead.