Member Avatar for mgirl

Hi,
My change counting program runs perfectly except the counters don't return to 0 when I try another number. The change adds up from the first try. Is there any way to fix this?

#include <stdio.h>

int main()
{
    int toonie=0, dollar=0, quarter=0, dime=0, nickel=0, cent=0;  
    float amount, change, cchange;
    char ch='Y', N, Y;
    
    while(ch=='Y'){
         
    printf("\n Your total is $", amount);
    scanf(" %f", &amount);
    
    change=(5.00-amount);
    printf("\n Your change is $%3.2f\n", change);
    while(change>=0 && change<5.00){
    cchange=(change*100);
    while (cchange>=200){
        cchange-=200;
        toonie++;
        }
    while (cchange>=100){
        cchange-=100;
        dollar++;
        }
    while (cchange>=25){
        cchange-=25;
        quarter++;
        }
    while (cchange>=10){
        cchange-=10;
        dime++;
        }
    while (cchange>=5){
        cchange-=5;
        nickel++;
        }
    while(cchange>=1){
        cchange-=1;           
        cent++;
        }
        
       // print results
       printf(" You have %d coins of 2 dollars \n", toonie);
       printf(" You have %d coins of 1 dollar \n", dollar);
       printf(" You have %d coins of 25 cents \n", quarter);
       printf(" You have %d coins of 10 cents \n", dime);
       printf(" You have %d coins of 5 cents \n", nickel);
       printf(" You have %d coins of 1 cent \n", cent);
       break;
       } // end while
       
       printf("\n Do you want to try again? (Y/N)\n\n");
       ch=toupper(getche());
       
       } // end while
       getch();
       
       }

thanks for your help!

Recommended Answers

All 5 Replies

Why not put the initializations in the loop?

Member Avatar for mgirl

Why not put the initializations in the loop?

I had thought of that but didn't know if it was ok to do it.
Thanks. It works!

#include <stdio.h>

int main()
{
    int toonie, dollar, quarter, dime, nickel, cent;  
    float amount, change, cchange;
    char ch='Y', N, Y;
    
    while(ch=='Y'){
    //-- init / reinit
    toonie = 0; dollar = 0; quarter = 0; dime = 0; nickel = 0; cent = 0;
    printf("\n Your total is $", amount);
    scanf(" %f", &amount);
    
    change=(5.00-amount);
    printf("\n Your change is $%3.2f\n", change);
    while(change>=0 && change<5.00){
    cchange=(change*100);
    while (cchange>=200){
        cchange-=200;
        toonie++;
        }
    while (cchange>=100){
        cchange-=100;
        dollar++;
        }
    while (cchange>=25){
        cchange-=25;
        quarter++;
        }
    while (cchange>=10){
        cchange-=10;
        dime++;
        }
    while (cchange>=5){
        cchange-=5;
        nickel++;
        }
    while(cchange>=1){
        cchange-=1;           
        cent++;
        }
        
       // print results
       printf(" You have %d coins of 2 dollars \n", toonie);
       printf(" You have %d coins of 1 dollar \n", dollar);
       printf(" You have %d coins of 25 cents \n", quarter);
       printf(" You have %d coins of 10 cents \n", dime);
       printf(" You have %d coins of 5 cents \n", nickel);
       printf(" You have %d coins of 1 cent \n", cent);
       break;
       } // end while
       
       printf("\n Do you want to try again? (Y/N)\n\n");
       ch=toupper(getche());
       
       } // end while
       getch();
       
       }

An unwritten rule in programming, if your not sure try it anyway and at worst case it wont work it will only unleash mutually assured destruction on us.

your counter will set to zero when u re-execute the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.