Ok, so I've been making my way through C for dummies, and I've run into something sort of weird....
Here's my source:
#include <stdio.h>
#define SPEEDLIMIT 55
#define RATE 15
#define FIRST_TICKET 85
#define SECOND_TICKET 95
#define THIRD_TICKET 100
int main()
{
int total,fine,speeding;
puts("Speeding Tickets\n");
/* first ticket */
speeding = FIRST_TICKET - SPEEDLIMIT;
fine = speeding * RATE;
total = total + fine;
printf("For going %d in a %d zone: $%d\n",FIRST_TICKET,SPEEDLIMIT,fine);
/* second ticket */
speeding = SECOND_TICKET - SPEEDLIMIT;
fine = speeding * RATE;
total = total + fine;
printf("For going %d in a %d zone: $%d\n",SECOND_TICKET,SPEEDLIMIT,fine);
/* third ticket */
speeding = THIRD_TICKET - SPEEDLIMIT;
fine = speeding * RATE;
total = total + fine;
printf("For going %d in a %d zone: $%d\n",THIRD_TICKET,SPEEDLIMIT,fine);
/* Display Total */
printf("\nTotal in fines: $%d\n",total);
getch();
return(0);
}
When I run this it says the total in fines is 1,727, when it should be 1,725. Where the hell is the extra 2 coming from??? I've got the source copied straight from the book exactly.
I'm compiling using Bloodshed Dev C++ 4.9.9.2
Posts
we also talk about other random shit and clown upon each other
One of the first things I got told in my C++ class was not to ever define variables that way. Each variable gets its own line and (if need be) it's own initializer at that point. So yeah... for clarity's sake as well as your own sanity maybe initialize variables.
Yeah, to elaborate, whenever you declare a variable, it comes with whatever garbage was already in that memory location. A lot of times, that can be 0 so you may not notice it. Other times, it can be almost anything.
If you're setting a variable = to something, you're overwriting whatever is in there, so you'll normally be okay (though it's always a good idea to initialize them all when first declared until you get the hang of them). In your case, you're setting it equal to itself + something, so it's adding whatever garbage may be there to your fine variable.
You can declare them like
without any problems.
Much like everything with learning programming, it's to teach you good habits early instead of trying to break them later. Granted, there's functionally nothing wrong with having a separate line for each variable, but as your program grows and you get more and more, it can become an eyesore. I personally will use separate lines with simple programs, but if I'm dealing with 20 variables of different types, I like to use one line per type to keep them organized. If you're the only one using the program, feel free to do it however you want. Feel free to do it anyway, but don't be surprised if someone later asks you why you did that. :P
I believe some compilers automatically initialize integers to 0 when the program doesn't specify a value. You shouldn't rely on that behavior even if yours does it, so that your code is portable (like this example) in case you or someone else want to use a different compiler.