As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Basic C programming help

TheMorningStarTheMorningStar Registered User regular
edited October 2007 in Help / Advice Forum
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

TheMorningStar on

Posts

  • Options
    DocDoc Registered User, ClubPA regular
    edited October 2007
    What do you mean when you say you "run this" and it tells you the number of lines? The compiler tells you the number of lines?

    Doc on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited October 2007
    I think he said "fines".

    Jasconius on
  • Options
    DocDoc Registered User, ClubPA regular
    edited October 2007
    haha, I'm an idiot. re-reading now.

    Doc on
  • Options
    DocDoc Registered User, ClubPA regular
    edited October 2007
    You never initialize "total" to 0. Gotta do that.

    Doc on
  • Options
    iTunesIsEviliTunesIsEvil Cornfield? Cornfield.Registered User regular
    edited October 2007
    haha... for some reason without initializing those 3 ints at the top MS's Visual C++ compiler was giving me $4221528. Funny stuff. If I initialize all 3 to 0, the program is just fine.

    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.

    iTunesIsEvil on
  • Options
    Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    edited October 2007
    Doc wrote: »
    You never initialize "total" to 0. Gotta do that.

    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.

    Sir Carcass on
  • Options
    Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    edited October 2007
    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.

    You can declare them like
    int total=0,fine=0,speeding=0;
    

    without any problems.

    Sir Carcass on
  • Options
    iTunesIsEviliTunesIsEvil Cornfield? Cornfield.Registered User regular
    edited October 2007
    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.

    You can declare them like
    int total=0,fine=0,speeding=0;
    

    without any problems.
    Meh... at that point (IMO) you might as well put them all on their own line. For me it's just a clarity thing. My noggin can parse
    int total = 0;
    int fine = 0;
    int speeding = 0;
    
    much more easily than all in-line like that. But, yes, the effect is obviously the same. :)

    iTunesIsEvil on
  • Options
    TheMorningStarTheMorningStar Registered User regular
    edited October 2007
    Huh. Thanks guys! I wonder why this damn book doesn't mention that...

    TheMorningStar on
  • Options
    Sir CarcassSir Carcass I have been shown the end of my world Round Rock, TXRegistered User regular
    edited October 2007
    Meh... at that point (IMO) you might as well put them all on their own line. For me it's just a clarity thing. My noggin can parse
    int total = 0;
    int fine = 0;
    int speeding = 0;
    
    much more easily than all in-line like that. But, yes, the effect is obviously the same. :)

    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

    Sir Carcass on
  • Options
    SmasherSmasher Starting to get dizzy Registered User regular
    edited October 2007
    Huh. Thanks guys! I wonder why this damn book doesn't mention that...

    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.

    Smasher on
Sign In or Register to comment.