Thread: help with a simple simple probram (i'm new)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    help with a simple simple probram (i'm new)

    Hi I am very very new to C, and am working on an assignment for a C programming intro class and I am having a little trouble on my program. The program is supposed to get the users change needed from the input of purchase price and cash tendered, and then print the amount of each coin needed to be given back in change. The problem is that when the program is executed, it gets only to the point of printing the change needed to be given, and then stops. please help me to find my stupid error.
    My code is as follows:

    Code:
    int main(int argc, char **argv)		/* Main declaration with arguments           */
    {
    char x;
    int pennies = 0, nickels = 0, dimes = 0, quarters = 0 , halfDollars = 0, dollars =0 , fiveDollars = 0 , tenDollars = 0;
    float purchasePrice , cashTendered , cashChange;
    
    //collects imput from user and finds the change needed from those two imputs.
    printf("Enter the purchase price including cents ($ddd.cc): $");  
    scanf("%f" , &purchasePrice);
    printf("Enter amount of cash tendered including cents ($ddd.cc): $");
    scanf("%f" , &cashTendered);
                     cashChange = cashTendered - purchasePrice;
    printf("Total Change: $%.2f\n" , cashChange); 
    
    //Finds each coin value for the change
    
    while (cashChange >= 00.00){
          while (cashChange >= 10.00){
                cashChange  = cashChange - 10.00 ;
                tenDollars = tenDollars + 1;
          }
          while (cashChange >= 5.00){
                cashChange  = cashChange - 5.00 ;
                fiveDollars = fiveDollars + 1;
          }
          while (cashChange >= 1.00){
                cashChange  = cashChange - 1.00 ;
                dollars = dollars + 1;
          }
          while (cashChange >= 0.50){
                cashChange  = cashChange - 0.50 ;
                halfDollars = halfDollars + 1;
          } 
          while (cashChange >= 0.25){
                cashChange  = cashChange - 0.25 ;
                quarters = quarters + 1;
          }    
          while (cashChange >= 0.10){
                cashChange = cashChange - 0.10 ;
                dimes = dimes + 1;
          } 
          while (cashChange >= 0.05){
                cashChange = cashChange - 0.05 ;
                nickels = nickels + 1;
          }   
          while (cashChange >= 0.01){
                cashChange = cashChange - 0.01 ;
                pennies =pennies + 1;
          }   
    }
    
    
    //prints the value of each coin
    printf("Ten Dollar Coins: %d\n" , tenDollars); 
    printf("Five Dollar Coins: %d\n" , fiveDollars);
    printf("One Dollar Coins: %d\n" , dollars);   
    printf("Half Dollar Coins: %d\n" , halfDollars); 
    printf("Quarter Coins: %d\n" , quarters);                 
    printf("Dime Coins: %d\n" , dimes);    
    printf("Nickel Coins: %d\n" , nickels);  
    printf("Penny Coins: %d\n" , pennies);  
    printf("[Done]\n");  
    
    //exits program
    printf(" \n");
        scanf("%c",&x);
        printf(" \n");
        
        printf("Press any key to continue . . . ");
        scanf("%c",&x);  
        exit(0);
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Do you have access to a debugger? This would demonstrate to you what is happening. Basically, the problem occurs as you break down the change - after a while, you have some change left, but it is less than one hundredth of a cent. So it is 'greater than 0.00', yet does not fit any of the categories which follow it. This is a difficulty of trying to compare floating point numbers.

    You ought to have a look at the following for some ideas about what is going on, and what you can do about it.

    comp.lang.c FAQ: What's a good way to check for ``close enough'' floating-point equality?

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    This next one might be a little helpful too:

    IEEE Standard 754 Floating Point Numbers
    Last edited by kermit; 10-09-2009 at 04:25 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    i'm sorry i still don't understand after reading those items, how would I then alter my program so that it rounds them down to the nearest hundredth?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Someone else might offer a better solution, but how about skipping the floating point comparisons, and doing something like have two variables to store your money in - one for the dollars, and one for the cents. You can then use an int type for both, and your comparisons will work as you want them to.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A good idea to to avoid floating point numbers if possible. An alternate solution could be to simply multiply your money by 100. Floating point numbers are inexact due to the way they work, and that causes problems, especially in comparisons.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void change(const float cash) {
      int ten_dollars  = 0;
      int five_dollars = 0;
      int one_dollar   = 0;
      int half_dollar  = 0;
      int quarters     = 0;
      int dimes        = 0;
      int nickels      = 0;
      int pennies       = 0;
    
      float small_denominations = cash - floor(cash);
      int __cash                = floor(cash);
    
      if (__cash > 0) {
        ten_dollars   = __cash / 10;
        __cash        = __cash % 10;
      }
    
      if (__cash > 0) {
        five_dollars  = __cash / 5;
        __cash        = __cash % 5;
      }
    
      one_dollar           = __cash;
    
      half_dollar          = small_denominations / 0.50f;
      small_denominations  = small_denominations - (half_dollar * 0.5f);
    
      quarters             = small_denominations / 0.25f;
      small_denominations  = small_denominations - (quarters * 0.25f);
    
      dimes                = small_denominations / 0.10f;
      small_denominations  = small_denominations - (dimes * 0.10f);
    
      nickels              = small_denominations / 0.05f;
      small_denominations  = small_denominations - (nickels * 0.05f);
    
      pennies              = small_denominations / 0.01f;
      small_denominations  = small_denominations - (pennies * 0.1f);
    
      printf("ten_dollars  == %d\n", ten_dollars);
      printf("five_dollars == %d\n", five_dollars);
      printf("one_dollar   == %d\n", one_dollar);
      printf("half_dollar  == %d\n", half_dollar);
      printf("quarters     == %d\n", quarters);
      printf("dimes        == %d\n", dimes);
      printf("nickels      == %d\n", nickels);
      printf("pennies      == %d\n", pennies);
    }
    
    int main() {
      float cash = 0;
      printf("Please enter the cash amount\n");
      scanf("%f", &cash);
    
      change(cash);
    
      return 0;
    }
    Look at the code above may be u can get some help from this

    As Elysia said avoid float that is correct because u can see i have to wrote something different from integer calculation but if it is needed than may be the above code can help u in that

    and as like every code can be optimized so may be this can be also

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM