Thread: Exercise 3 --- Correct Change

  1. #16
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Okay. Will do.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What do you mean, Jim?
    I mean that you can do this assignment without any if() statements. Something like:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int dollars = 0;
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
    
        printf("Please enter the starting amount in pennies: ");
        scanf("%d", &pennies);
        
        dollars = pennies / 100;
        pennies = pennies - (100 * dollars);
    
        quarters = pennies / 25;
        pennies = pennies - (25 * quarters);
    
        dimes = pennies / 10;
        pennies = pennies - (10 * dimes);
    
        nickels = pennies / 5;
        pennies = pennies - (5 * nickels);
    
     
        printf("Your change is : %d dollars, %d quarters, %d dimes, %d nickels, and %d pennies.\n", dollars, quarters, dimes, nickels, pennies);
     
        return(0);
    }
    Jim

  3. #18
    Registered User
    Join Date
    Oct 2015
    Posts
    42
    Oh, ok. So when do you suggest I use the if statement and when not to use it?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on your requirements. If you need to avoid printing when the result is 0, then an if statement would be appropriate. If you will print anyway when the result is 0, no if statement is needed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-28-2015, 10:53 AM
  2. Change Calculator with Breakdown of the Change
    By insertcoin in forum C Programming
    Replies: 2
    Last Post: 04-16-2014, 08:25 AM
  3. little help with a C exercise
    By Talon320 in forum C Programming
    Replies: 29
    Last Post: 09-26-2013, 11:01 PM
  4. Replies: 2
    Last Post: 02-16-2012, 03:03 PM
  5. Help exercise!
    By sanhthai in forum C++ Programming
    Replies: 10
    Last Post: 09-22-2009, 12:07 PM