![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
2 problems
Hello, I am writing this program whose function is to insert coins into a machine and I don't know how to perform this actions:
#include <stdio.h>
main ()
{
int coins, i, result;
for (i=1; i<=1000; i++)
{
printf ("Print 5 to insert a coin of 5 cents, 10 for the 10 cents coin, 25 for the 25cents, 50 for the 50cents, 100 for the 1 peso and -1 to close the process of insert coins: \n ");
if (coins==-1)
break;
scanf ("%d", &coins);
}
result=coins;
printf ("The total money put in the machine is:%d", result); }1) I am using the "if (coins==-1)" followed for the break statement to interrupt my coin loop, but if I do that, my program will store -1 in the coin variable and print the total money as -1, instead of previously supplying to me the result of the addition of the values of the inserted coins. 2)Even if I use a loop with like this "for=1; i<=5; i++" and didn't use "if coins==-1", as in this case I would not need to interrupt the loop before it's end, I still don't get the correct value of the "total money" because at each loop interaction the coin that I insert in the machine overwrite the previous value. So what I need to know is how to use correctly the "if (coins==-1) to interrupt my semi-infinite loop of 1000 interactions at any point, and then print on the screen the correct total amount of money that I inserted in the machine throught a insertion of several coins, using something that could be this: "result= coin1(50 cents) + coin2 (25 cents) + coin 3 (100 cents) + coin 4 (5 cents).....until a x limit that is changeable at each time. Summarizing, I must know how to add the values of a changeable amount of currencies to get the total inserted money in the machine. Thanks for the help. ![]() |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
You just need to change the order you are doing things.
If you do this -1 will not get added to result because you break before the addition pseudo code printf scanf if(coins == -1) break; result = result + coins; I don't quite understand your 2nd issue to display details of what was entered I'd keep a counter of each coin type and increment as you go int dime; dime++ then your output can be something like total: $1.23 3 quarter 4 dimes 1 nickles 3 pennies Last edited by spydoor; Mar 23rd, 2005 at 4:43 PM. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
switch(coin){
case 1:
cent+;+;
break
case 10:
dime++;
break;
case 25:
quater++;
break;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|