Thread: why doesnt this small program work?

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

    why doesnt this small program work?

    I am making a program for a class. The program is supposed to calculate the change needed in coins from the amount given. The problem is that it gets stuck somewhere in the coin sorting loop but I can't figure out why or where. help is appreciated.
    Code:
    int main(int argc, char **argv)		/* Main declaration with arguments           */
    {
    char x;
    int multipliedCashChange, 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;
                     multipliedCashChange = cashChange * 100;
    printf("Total Change: $%.2f\n" , cashChange); 
    
    //Finds each coin value for the change
    
    
    while (multipliedCashChange >= 0){
          while (multipliedCashChange >= 1000){
                multipliedCashChange  = multipliedCashChange - 1000 ;
                tenDollars = tenDollars + 1;
          }
          while (multipliedCashChange >= 500){
                multipliedCashChange  = multipliedCashChange - 500 ;
                fiveDollars = fiveDollars + 1;
          }
          while (multipliedCashChange >= 100){
                multipliedCashChange  = multipliedCashChange - 100 ;
                dollars = dollars + 1;
          }
          while (multipliedCashChange >= 50){
                multipliedCashChange  = multipliedCashChange - 50 ;
                halfDollars = halfDollars + 1;
          } 
          while (multipliedCashChange >= 25){
                multipliedCashChange  = multipliedCashChange - 25 ;
                quarters = quarters + 1;
          }    
          while (multipliedCashChange >= 10){
                multipliedCashChange = multipliedCashChange - 10 ;
                dimes = dimes + 1;
          } 
          while (multipliedCashChange >= 5){
                multipliedCashChange = multipliedCashChange - 5 ;
                nickels = nickels + 1;
          }   
          while (multipliedCashChange >= 1){
                multipliedCashChange = multipliedCashChange - 1 ;
                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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't need the final loop, because once you're less than 5, the remainder is your 1s.

    Anyway, when you find yourself stuck in a loop, especially when you have many of them, throw some printf statements in, so you can see what spot you're getting stuck at. You can also shorten:
    Code:
    x = x - 1;
    /* to... */
    x--;
    /* or addition... */
    x++;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Thumbs up Solution!

    SeanDugan890...Did you find it? It is easier to find than I thought and quzah`s suggestion takes you right to it although I had a sneaking suspicion before I printf`ed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program doesn't work on others comps
    By Livijn in forum C# Programming
    Replies: 8
    Last Post: 10-29-2008, 06:03 AM
  2. Simple program won't work on a friend's comp
    By Ouendanation in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2008, 08:03 PM
  3. Why this program doesnt work properly
    By enggabhinandan in forum C Programming
    Replies: 12
    Last Post: 10-21-2006, 04:12 AM
  4. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 11:40 AM
  5. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM