C++ change counter program question

This is a discussion on C++ change counter program question within the C++ Programming forums, part of the General Programming Boards category; Hi, I have an assignment to create a change counter program. Once I have written the code, and tested it, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    C++ change counter program question

    Hi,

    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?

    Thanks

    Here's the code.

    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)) ;
    
            //Twenties
    			printf("Twenties: %d\n", changeInCents / VALUE_OF_TWENTY);
    			changeInCents %= 2000;
    
    		//Tens
    			printf("Tens: %d\n", changeInCents / VALUE_OF_TENS);
    			changeInCents %= 1000;
    
    		//Fives
    			printf("Fives: %d\n", changeInCents / VALUE_OF_FIVES);
    			changeInCents %= 500;
    
    		//Ones
    			printf("Ones: %d\n", changeInCents / VALUE_OF_ONES);
    			changeInCents %= 100;
    
    		//Quarters
    			printf("Quarters: %d\n", changeInCents / VALUE_OF_QUARTER);
    			changeInCents %= 25;
    
    		//Dimes
    			printf("Dimes: %d\n", changeInCents / VALUE_OF_DIMES);
    			changeInCents %= 10;
    
    		//Nickles
    			printf("Nickles: %d\n", changeInCents / VALUE_OF_NICKLES );
    			changeInCents %= 5;
    
    		//Pennies
    			printf ("Pennies: %d\n", changeInCents);
    			
    		
    
    
            getchar();
            getchar();
            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
    Location
    Tomball, TX
    Posts
    407
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,781
    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?


    Jim

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21