Dec 15, 2011

Return change in C++


Have you paid a bill in a vending machine and wondered how the program will change them back? Most likely not, but it turns out that the teacher of Michelangelo was commissioned a program to simulate that, in his own words:
"Suppose a coin machine 10,5,2,1 and 50 cents. You write a program that will decide how many coins you exchange, giving the highest priority designation. You should receive as input the amount of give.Example: for $ 40.50 will be October 4, 0 5, 0 2, 1 and 1 0 of 50 cents. "
Cambio
So the first problem arises how to input the user to enter only amounts like $ 50 or $ 43.50 and $ 23.45 or amounts as $ 9.70?
What I did is ask for the amount in a float variable (change), another variable that amount to convert int (cambioint). In the process, lose the decimal part of number, so if the change remains - cambiointis equal to 0 or equal to 0.50, the number the user entered is correct.
The other part is simple, only needs to see if the variable rate is above 10. If yes, you subtract 10 to the variable and increases a counter. Then do the same with the other currencies.
But hey, the program is this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 # Include <iostream>
 using namespace std;

 int main ()
 {
     change float, int cambioint, m10 = 0, m5 = 0, m2 = 0, m1 = 0, m50c = 0;
     do
     {
         court <<"Change?:" cin>> change;
         cambioint = (int) return;
     } While ((change - cambioint)! = 0 & & (change - cambioint)! = 0.50);
     while (change! = 0)
     {
         IF (shift> = 10)
         {
             m10 + +;
             change - = 10;
         }
         else if (change> = 5)
         {
             m5 + +;
             change - = 5;
         }
         else if (change> = 2)
         {
             m2 + +;
             change - = 2;
         }
         else if (change> = 1)
         {
             m1 + +;
             change - = 1;
         }
         else if (change> = 0.5)
         {
             m50c + +;
             change - = 0.5;
         }
     }
     court <<m10 <<"" <<m5 <<"" <<m2 <<"" <<m1 <<"" <<m50c;
 } 
Would lack only give a little formatted output, but it is already a matter of taste and you know, any questions here are the comments below.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes