Thread: C++ change counter program question

  1. #1
    Registered User
    Join Date
    Oct 2010

    C++ change counter program question


    I have an assignment to create a change counter program. Once I have written the code, and tested it, I must enter in the GDP and money the US spends on gas to see how much is left over.

    The thing is the program goes a little wacky in the higher digit numbers. I'm just wondering exactly why. So...why?


    Here's the code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define VALUE_OF_TWENTY   2000
    #define VALUE_OF_TENS     1000
    #define VALUE_OF_FIVES     500
    #define VALUE_OF_ONES      100
    #define VALUE_OF_QUARTER    25
    #define VALUE_OF_DIMES      10
    #define VALUE_OF_NICKLES     5
    #define VALUE_OF_PENNIES     1
    #define REMAINDER_VALUE	    .5
    int main(void)
           long double amountOfPurchase = 0.0;
           long double amountTendered   = 0.0;
    	   long	double changeInDollars  = 0.0;
    	   unsigned int changeInCents       = 0.0;
    		printf("Cash Register\n\n");
            printf("Please enter the total amount of purchase: $");
            scanf("%lf", &amountOfPurchase);
            printf("Please enter amount of money tendered: $");
            scanf("\n\n%lf", &amountTendered);
    		if(amountTendered < amountOfPurchase)
    			changeInDollars = amountTendered - amountOfPurchase;
    			printf("You still owe money. $%.2lf\n\n", changeInDollars);
    		if(amountTendered > amountOfPurchase);
    			changeInDollars = amountTendered - amountOfPurchase;
    			printf("\nYour change is $%.2lf\n\n", changeInDollars);
    		changeInCents = (int)( floor(changeInDollars * 100 + REMAINDER_VALUE)) ;
    			printf("Twenties: %d\n", changeInCents / VALUE_OF_TWENTY);
    			changeInCents %= 2000;
    			printf("Tens: %d\n", changeInCents / VALUE_OF_TENS);
    			changeInCents %= 1000;
    			printf("Fives: %d\n", changeInCents / VALUE_OF_FIVES);
    			changeInCents %= 500;
    			printf("Ones: %d\n", changeInCents / VALUE_OF_ONES);
    			changeInCents %= 100;
    			printf("Quarters: %d\n", changeInCents / VALUE_OF_QUARTER);
    			changeInCents %= 25;
    			printf("Dimes: %d\n", changeInCents / VALUE_OF_DIMES);
    			changeInCents %= 10;
    			printf("Nickles: %d\n", changeInCents / VALUE_OF_NICKLES );
    			changeInCents %= 5;
    			printf ("Pennies: %d\n", changeInCents);
            return EXIT_SUCCESS;
    Last edited by Salem; 10-07-2010 at 05:29 AM. Reason: Fixed the damn tags!

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Tomball, TX
    Can you please use code tags next time? I also wonder why so much C and not C++, but moving along. How big are the numbers that are messing the program up? You might be going out of range. Nothing from the code is jumping out at me sytax wise other than the printfs and scanfs and getchar()s in a C++ program, sorry.
    Virtual reality hello world in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    May 2010
    I noticed a several things wrong. First why long double? Second If this is a C++ program why use scanf, printf? Third long double is "%Lf" in both scanf and printf (note Captial).

    What does
    little wacky
    mean? What do you expect? What are you getting?


  4. #4
    Registered User
    Join Date
    Oct 2010
    I've never taken a computer science class before, and I have done zero programming prior to this.
    My class is the first in a sequence of C++ courses. For the first quarter the teacher is basically teaching C and is surprisingly rigid for, what I'd expect, a comp sci class to be, thus I'm required to use the scanf and printf, and etc. The code is still in the works as far as what will be turned in when its due, but the structure should be complete.

    Basically the code fails in the trillions. I'd like a technical explanation for this.

    Thanks for the posts

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    if(amountTendered > amountOfPurchase);

    This trailing ; is bad news for your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program to Calculate Change
    By CaptMorgan in forum C Programming
    Replies: 8
    Last Post: 04-07-2010, 12:31 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Question on purchase and change program
    By s_ny33 in forum C Programming
    Replies: 10
    Last Post: 11-07-2005, 03:14 PM
  4. Need help with some C programs. (VERY Long Post)
    By McFury in forum C Programming
    Replies: 9
    Last Post: 04-30-2004, 12:33 PM
  5. perl program question
    By newbie2c in forum Tech Board
    Replies: 2
    Last Post: 02-03-2003, 09:19 AM