Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2005, 3:56 PM   #1
colt
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 colt is on a distinguished road
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.
colt is offline   Reply With Quote
Old Mar 23rd, 2005, 4:40 PM   #2
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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.
spydoor is offline   Reply With Quote
Old Mar 24th, 2005, 2:19 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
switch(coin){
      case 1:
           cent+;+;
           break
      case 10:
          dime++;
          break;
      case 25:
          quater++;
          break;
}
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC