| | |
Returning counters to 0 ?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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?
thanks for your help!
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();
} 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
c Syntax (Toggle Plain Text)
#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.
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: implicit casting
- Next Thread: help me in array
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






