Bad code or bad compiler?

This is a discussion on Bad code or bad compiler? within the C Programming forums, part of the General Programming Boards category; Hello. I've just begun learning C and I've been practicing a few user input - calculation output programs. I got ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Talking Bad code or bad compiler?

    Hello. I've just begun learning C and I've been practicing a few user input - calculation output programs. I got one program down that's doing fine... but I have one piece of code that's making me wonder if I'm missing something or if it's the compiler.

    First, here's the code:

    Code:
    #include <stdio.h>
    
    #define bleacher 5
    #define	rsrvd 8
    #define	box 12
    
    FILE*fileout;
    
    void main (void)
    {
     int
    	bleachnum,
    	rsrvnum,
    	boxnum,
    	bleachrev,
    	rsrvrev,
    	boxrev,
    	totalrev;
    
    fileout = fopen ("A:RESULT.TXT","w");
    printf ("\nPlease type in the number of bleacher seats sold:  ");
    scanf ("%d",&bleachnum);
    printf ("\nPlease type in the number of reserved seats sold:  ");
    scanf ("%d",&rsrvnum);
    printf ("\nPlease type in the number of box seats sold:  ");
    scanf ("%d",&boxnum);
    
    printf ("\nYou have entered %d bleacher seats, %d reserved seats and %d box seats.", bleachnum, rsrvnum, boxnum);
    printf ("\n \n");
    printf ("\nReport created at A:RESULT.TXT.");
    
    bleachrev = bleacher * bleachnum;
    rsrvrev = rsrvd * rsrvnum;
    boxrev = box * boxnum;
    totalrev = bleachrev + rsrvd + boxrev;
    
    fprintf (fileout, "\nThe bleacher seats generated $%d.", bleachrev);
    fprintf (fileout, "\nThe reserved seats generated $%d.", rsrvrev);
    fprintf (fileout, "\nThe box seats generated $%d.", boxrev);
    fprintf (fileout, "\nThe total revenue for the game is $%d.", totalrev);
    fclose (fileout);
    }
    Well that's pretty much it. File generates fine, user input values come out fine. The only problem is that when I take a look at the RESULT.TXT file, the calculations become messed up.

    BTW, I have inputed 5000, 7000, 8000, respectively, for all the values it asks.

    For example, the line "box seats generated", it would say it generated $-1200. While the lines for bleacher and box seats would calculate fine. Also, the total revenue line would also have a negative result.

    I tried this using Miracle C compiler for Windows.

    I then tried compiling it under LCC-WIN32 compiler. This one compiled a little better. All the calculation result showed up correctly except the total revenue value was messed up. It gave some obscure number, 62098... which is questionable since theres no way the formula

    totalrev = bleachrev + rsrvd + boxrev

    would generate that number after inputting 5000, 7000, and 8000.

    Anyways, is the code fine? Compiler gone mad?
    Any recommendations on what Compiler I should use? I know alot of people recommend Linux compilers but I know just about nothing in operating Linux, atleast for now. Thanks.

  2. #2
    Unregistered
    Guest
    I think you may be running into overflow with your integers... depending on a few different factors (OS and compiler, probably), integers will only have a certain range of values (determined by the number of bytes attached to an int... I think you can find out for your system using the "sizeof()" function).

    I compiled and ran your code using gcc compiler and a Windows 2000 machine, and got

    The bleacher seats generated $25000.
    The reserved seats generated $56000.
    The box seats generated $96000.
    The total revenue for the game is $121008.

    As my output. So, check the size of an integer for your os/compiler... or just use a type of variable that has a larger range.

    Hope that helps.

    n9ski

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Two things...
    1. Your first problem, with the negative values, was probably some kind of overflow. ANSI C only requires that an int be able to hold values up to 32767, which is obviously not large enough for this application. longs must be able to reach 2147483647, which should be large enough for your purposes. Switch your code to use longs instead of ints. (This also means replacing all occourances of %d with %ld).

    2. This is wrong...
    Code:
    totalrev = bleachrev + rsrvd + boxrev;
    rsrvd? Are you sure you don't want to use rsrvrev instead of rsrvd?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Ah yes

    That's one of the problem... I'm going to have to use understrikes from now on.

    Thank you for all that replied

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad performance for the code to send Http request
    By Checker1977 in forum C# Programming
    Replies: 8
    Last Post: 08-20-2008, 08:16 PM
  2. Compiler Optimized code
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-18-2004, 02:07 PM
  3. Book- bad code :( me ish unhappyor
    By CodeMonkey in forum Game Programming
    Replies: 7
    Last Post: 07-09-2003, 11:29 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 04:04 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21