#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Help Testing a Simple Program


    I'm taking a programming class and need to create a simple change maker. Unfortunately, this assignment is due today and I still haven't been able to install the necessary software to run and test my work. Can someone please do me a big favor and test out what I have? I'm a novice programmer, so there may be mistakes and I'd really appreciate suggestions or any help.

    Here's what I have, it should print the change amount entered and the number of each type of change to produce said amount (if a type of change is 0, it shouldn't print it at all):
    // Project 1, Change Maker
    #include <stdio.h>

    // Function main begins program execution
    int main (void)
    {
    int changeamount; // change amount entered by user
    int quarters; // number of quarters
    int dimes; // number of dimes
    int nickels; // number of nickels
    int pennies; // number of pennies

    printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
    scanf ( "%d", &changeamount ); // read an integer

    if ( changeamount < 1 )
    {
    printf ( "Change amount must be between 1 and 99.\n" );
    } // end if

    if ( changeamount > 99 )
    {
    printf ( "Change amount must be between 1 and 99.\n" );
    }

    if ( 0 < changeamount && changeamount < 100 )
    {
    if ( changeamount >= 25 )
    {
    quarters = changeamount / 25 // assign total to quarters
    changeamount = changeamount % 25 // update changeamount
    printf ( "Quarters: %d\n", quarters ); // print number of quarters
    }

    if ( changeamount >= 10 )
    {
    dimes = changeamount / 10 // assign total to dimes
    changeamount = changeamount % 10 // update changeamount
    printf ( "Dimes: %d\n", dimes ); // print number of dimes
    }

    if ( changeamount >= 5 )
    {
    nickels = changeamount / 5 // assign total to nickels
    changeamount = changeamount % 5 // update changeamount
    printf ( "Nickels: %d\n", nickels ); // print number of nickels
    }

    if ( changeamount >= 1 )
    {
    pennies = changeamount / 1 // assign total to pennies
    changeamount = changeamount % 1 // update changeamount
    printf ( "Pennies: %d\n", pennies ); // print number of pennies
    }
    }

    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    I already see a problem but I'm not sure how to fix it. If the user enters a number greater than 99, it'll say "Change amount must be between 1 and 99," but then will continue to run the rest of the program. How do I end it if a number between 1 and 99 isn't entered?

    Edit: I updated it with another if then statement, but I'm still not sure if it'll work.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    1. Use code tags! Since you didn't, HTML removed all the leading spaces in your code, thus removing its formatting, and turned it into an unreadable mess. If you use code tags, then that will not happen. Rest assured that you will hear about from us every time you don't use code tags

    It's very simple. These are what code tags look like: [code] [/code] All you need to do is to copy-and-paste your formatted code between them. From now on, do not forget to use them. For that matter, go back to your first message and add them.

    2. You broke your promise to the compiler. You told the compiler that your main function would return an int (which is correct), and yet you did not return anything. The last line executed in main needs to be a return 0; -- returning a zero indicates successful completion of the program, whereas returning a non-zero indicates that you terminated the program due to an error condition (eg, a file it was supposed to open and read could not be opened).

    3. Since your having left out that return statement must have caused the compiler to issue at least a warning, that means that you must be ignoring warnings. Never ignore warnings! Warnings are much more important than error messages are. You must never ignore warnings!

    Originally Posted by Michael Carl
    If the user enters a number greater than 99, it'll say "Change amount must be between 1 and 99," but then will continue to run the rest of the program. How do I end it if a number between 1 and 99 isn't entered?
    Consider this part of your program (also note the use of code tags):
    Code:
        printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
        scanf ( "%d", &changeamount ); // read an integer
    
        if ( changeamount < 1 )
        {
            printf ( "Change amount must be between 1 and 99.\n" );
        } // end if
    
        if ( changeamount > 99 )
        {
            printf ( "Change amount must be between 1 and 99.\n" );
        }
    First, instead of two separate tests, why not combine them? Implicitly you are saying "if changeamount is less than 1 or changeamount is greater than 99", so why not say it explicitly? BTW, the relational OR operator is ||.

    Then once you have combined those two if-statements into one, use the if-else construct thus:
    Code:
        printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
        scanf ( "%d", &changeamount ); // read an integer
    
        if ( changeamount < 1 || changeamount > 99 )
        {
            printf ( "Change amount must be between 1 and 99.\n" );
        }
        else
        {
            // into here insert "the rest of the program"
        }
    One basic approach to designing a program is to think about its overall structure, write that overall structure, and then fill in the blanks with code.

    PS
    As Larry Wall, creator of Perl ("Perfectly Eclectic Rubbish Lister", once the language of choice in web programming), said: "There's more than one way to do it."

    What a C programmer would typically do would be to return with an error as soon as an error is detected, so that the only way you can get further down the code would be if you had not encountered an error. So, keeping your original structure, that would look like this:
    Code:
        printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
        scanf ( "%d", &changeamount ); // read an integer
    
        if ( changeamount < 1 )
        {
            printf ( "Change amount must be between 1 and 99.\n" );
            return 1; // a non-zero to indicate failure; ie, termination due to an error
        } // end if
    
        // getting this far means that changeamount is not less than 1
    
        if ( changeamount > 99 )
        {
            printf ( "Change amount must be between 1 and 99.\n" );
            return 2; // a non-zero to indicate failure; ie, termination due to an error
        }
    
        // getting this far means that changeamount is valid; it's within the limits of 1 to 99, inclusive
        //  therefore, you're good-to-go for successful completion of the program
    
        if ( 0 < changeamount && changeamount < 100 )
        {
            if ( changeamount >= 25 )
            {
    This is the way that actual C programmers write code. However, it violates the rules of structured programming -- specifically the rule that each function has only one entry point and only one exit point -- and so your instructor will not like it.

    Write properly structured code as you are taught, but recognize what another programmer is doing when you encounter code like this.
    Last edited by dwise1_aol; February 14th, 2013 at 07:16 PM.

IMN logo majestic logo threadwatch logo seochat tools logo