www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/22/01:26:04

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problems with Scanf in Allegro.WAS -Problems with palette.
Date: Fri, 21 Feb 1997 00:40:44 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 80
Message-ID: <330D5F8C.1B6C@cs.com>
References: <nQgAZIAVUODzEwI3 AT jenkinsdavid DOT demon DOT co DOT uk> <lPKw+OA9XRDzEwY5 AT jenkinsdavid DOT demon DOT co DOT uk>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp206.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

David Jenkins wrote:
> 

These problems have nothing to do with Allegro.  I suggest you get out
your handy C reference, because you're making quite a few mistakes.

> void            main()

This must be 'int main()' to be ANSI C compliant.  You must also put a
'return 0;' statement at the end of main().  In C++, "void main()" will
give you a warning.  Where did you learn to use 'void'?

> {
> 
> /* If I un // the next two lines and // the "int ns = 100;" line I
> should be able to enter the number of stars.BUT I keep getting errors
> during compiling.Any ideas???
> */
> 
> //    printf("How many stars shall I use???");
> //    scanf("%d", ns);

First, you have to declare ns before you can use it.  This is C, not
BASIC.  Second, scanf must be passed a pointer to the variable to be
inputted, not the variable itself.  So, the correct code would look
like:

    int ns;
    scanf( "%d", &ns );

Also, are you writing in C or C++?  You cannot put variable declarations
in the middle of a block of C code.  C++ is more forgiving of this, but
if you are writing C++ you should be using 'cin' and 'cout', not
printf() and scanf().  You are also using C++-style comments, which is
not recommended in C programs.

>     int             d = 1;
>     int             o = 1;

If you are using these later as control variables for a for loop, you
don't need to initialize them here.  It doesn't look like you use 'o' at
all, in fact.

>     int             x[ns];
>     int             y[ns];
>     int             sp[ns];

This may work under GNU C, but it's EXTREMELY unportable!  You should
try to avoid defining arrays of variable length unless there is no other
way to do what you want.  In this case, if the user inputs an invalid
number of stars (for example, -100), your program will crash and may
take your computer with it.  Here's a much safer way to do the above:

int x = malloc( ns * sizeof(int) );
int y = malloc( ns * sizeof(int) );
int sp = malloc( ns * sizeof(int) );

Of course, if this is C code, those definitions must go at the beginning
of main(), and you can allocate memory for them later.  You should also
check to see whether malloc() worked, and if not, exit with an error.

[snip correct code]

>     exit(0);

Ugh.  Why, oh why, are you doing this the hard way?!?  Just use 'return
0;' here instead of exit().  There's no need to try to bypass the rules
of C, especially when the workaround is just as much if not more work. 
Where in the name of all that's holy did you learn to use void
main()/exit(0) instead of int main()/return 0;?  It's terrible
programming!

hth

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |        fighteer AT cs DOT com          |
| Proud owner of what might one   |   http://www.cs.com/fighteer    |
| day be a spectacular MUD...     | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019