Albert wrote, On 24/09/07 00:55:
Here's the code: (it's not mine - i'm analysing how people make simple
games in C) (see, i found the code but when i compiled it with digital
mars compiler it didn't know of the gotoxy function - i googled for a
compiler whose library defined the gotoxy function - namely the lcc
compiler but that didn't compile - i found out in the help they
defined it not in the conio.h but in tcconio.h but now it says the
error which was my original question - sigh)
There is nothing in your code to cause the error you are reporting with
any sane implementation and I don't think that Jacob's compiler is so
bad as to produce that error for the code you have posted.
As I suggested, your problem is with the use of non-standard facilities,
tcconio.h and clrscr. For those comp.compilers.lcc or a DOS group would
be appropriate. I will, however, point out the problems with your C code
which you should fix anyway.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <tcconio.h>
int x = 40, l, k = 2, z = 0, i, score = 0;
These should not be at file scope. They should be local to the top level
function they are needed in and passed as parameters. Then people can
actually see what the coupling is between functions. Also, even on the
rare occasions when file scope (or global) variables are needed, use
meaningful names, not things like i, z, k etc.
void gameover();
void game();
void top();
void shoot();
void gotoxy (int x, int y);
void main()
The only return type for main specified by the standard is int. Why use
something non-portable that gives you no benefit?
int main(void)
Being explicit about no parameters is also a good habit.
{
int ch;
clrscr();
printf("\n\t\t\t\tSHOOTING IN C");
printf("\n\t\t\t\t*************");
printf("\n\n\n1.NEW GAME\n2.INSTRUCTIONS\n3.QUIT");
printf("\n\n\n ENTER YOUR CHOICE:");
Without flushing the output or ending it with a newline the user might
not get to see your prompt in time.
IF you are going to use scanf, check the return value. It is there for a
good reason. Better would be using fgets to get the line and then
processing it after. With fgets you also have to check the return value,
but it is far easier to use correctly.
switch(ch)
{
case 1:
game();
break;
case 2:
clrscr();
printf("\n\t\t1. Press any key to start the game.");
printf("\n\t\t2. Use'n' for left and 'm' for right directions.");
printf("\n\t\t3. Press ENTER key for shooting");
printf("\n\t\t4. PRESS 'x' KEY for Exit");
printf("\n\n\n\t\t\t Don't Use any other keys");
getch();
getch is a non-standard function.
Why on earth call main recursively rather than using a simple loop? This
entire menu system needs to be properly reviewed.
This is a non-portable exit value. The only portable values are 0,
EXIT_SUCCESS and EXIT_FAILURE, the latter two being defined in stdlib.h
and 0 being another way of saying "success".
You can reach here, so you should return a value.
}
void game()
{
char n, c;
int k, y = 24;
k isn't used.
clrscr();
i = 0;
n = (char)i;
The cast is pointless.
clrscr();
gotoxy(12,25);
cprintf("\nUSE 'n' or 'm' For Moving and then press Enter for
shoot");
while(c!=120)
{
c = getch();
switch(c)
{
case 'n':
top();
gotoxy(x--, y);
shoot();
break;
case 'm':
top();
gotoxy(x++,y);
cprintf(" ");
shoot();
}
cprintf("%c ",n);
}
}
void top()
{
char n;
l = 254;
n = (char)l;
This cast is not needed.
randomize is not standard. To seed the random number generator use
srand((unsigned int)time(NULL));
<snip>
These are all serious points which need fixing, but none of them is the
cause of your immediate problem. How to use tcconio.h and the
non-standard functions is something that belongs else where.