![]() |
| | #1 |
| Registered User Join Date: Oct 2001
Posts: 11
| 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);
}
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 | |
| | #2 |
| 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 |
|
| | #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; |
| QuestionC is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |