C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-22-2001, 04:30 PM   #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.
musayume is offline   Reply With Quote
Old 10-22-2001, 04:42 PM   #2
Unregistered
Guest
 
Posts: n/a
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
  Reply With Quote
Old 10-22-2001, 06:50 PM   #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?
QuestionC is offline   Reply With Quote
Old 10-22-2001, 09:08 PM   #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
musayume is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:58 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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