C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2013, 03:13 PM
Michael Carl Michael Carl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Michael Carl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 44 m 54 sec
Reputation 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
}
}

}

Reply With Quote
  #2  
Old February 14th, 2013, 03:28 PM
Michael Carl Michael Carl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 Michael Carl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 44 m 54 sec
Reputation 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.

Reply With Quote
  #3  
Old February 14th, 2013, 06:57 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
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!

Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help Testing a Simple Program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap