Returning counters to 0 ?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 5
Reputation: mgirl is an unknown quantity at this point 
Solved Threads: 0
mgirl mgirl is offline Offline
Newbie Poster

Returning counters to 0 ?

 
0
  #1
Nov 6th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Returning counters to 0 ?

 
0
  #2
Nov 6th, 2008
Why not put the initializations in the loop?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: mgirl is an unknown quantity at this point 
Solved Threads: 0
mgirl mgirl is offline Offline
Newbie Poster

Re: Returning counters to 0 ?

 
0
  #3
Nov 6th, 2008
Originally Posted by Dave Sinkula View Post
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Returning counters to 0 ?

 
0
  #4
Nov 7th, 2008
  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int toonie, dollar, quarter, dime, nickel, cent;
  7. float amount, change, cchange;
  8. char ch='Y', N, Y;
  9.  
  10. while(ch=='Y'){
  11. //-- init / reinit
  12. toonie = 0; dollar = 0; quarter = 0; dime = 0; nickel = 0; cent = 0;
  13. printf("\n Your total is $", amount);
  14. scanf(" %f", &amount);
  15.  
  16. change=(5.00-amount);
  17. printf("\n Your change is $%3.2f\n", change);
  18. while(change>=0 && change<5.00){
  19. cchange=(change*100);
  20. while (cchange>=200){
  21. cchange-=200;
  22. toonie++;
  23. }
  24. while (cchange>=100){
  25. cchange-=100;
  26. dollar++;
  27. }
  28. while (cchange>=25){
  29. cchange-=25;
  30. quarter++;
  31. }
  32. while (cchange>=10){
  33. cchange-=10;
  34. dime++;
  35. }
  36. while (cchange>=5){
  37. cchange-=5;
  38. nickel++;
  39. }
  40. while(cchange>=1){
  41. cchange-=1;
  42. cent++;
  43. }
  44.  
  45. // print results
  46. printf(" You have %d coins of 2 dollars \n", toonie);
  47. printf(" You have %d coins of 1 dollar \n", dollar);
  48. printf(" You have %d coins of 25 cents \n", quarter);
  49. printf(" You have %d coins of 10 cents \n", dime);
  50. printf(" You have %d coins of 5 cents \n", nickel);
  51. printf(" You have %d coins of 1 cent \n", cent);
  52. break;
  53. } // end while
  54.  
  55. printf("\n Do you want to try again? (Y/N)\n\n");
  56. ch=toupper(getche());
  57.  
  58. } // end while
  59. getch();
  60.  
  61. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Returning counters to 0 ?

 
0
  #5
Nov 8th, 2008
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.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: Returning counters to 0 ?

 
0
  #6
Nov 12th, 2008
your counter will set to zero when u re-execute the program.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC