If I end my cost of .60 cents with a NICKLE or a DIME I get a 2.####.E number.
If I end the total cost with a quarter I don't get a big number ( Wrap around number right ? )
Can anyone tell me why?
#include <cstdlib>
#include <iostream>
char insert;
float cost = .60, change; // cost of soda is .60
using namespace std;
void vendingmachine();
void vendingprocess();
void vendingmachine()
{
cout << "Welcome to the vending machine!\n";
do{
cout << "Insert money\n";
cout << "[n]ickle\n[d]ime\n[q]aurter\n[o]ne dollar\n\n";
cin >> insert;
switch(insert)
{
case 'n':
cout << "You insert one nickle into the vending machine.\n";
cost = cost - .05;
cout << "Remaining balance:\t" << cost << endl;
break;
case 'd':
cout << "You insert one dime into the vending machine.\n";
cost = cost - .10;
cout << "Remaining balance:\t" << cost << endl;
break;
case 'q':
cout << "You insert one quarter into the vending machine.\n";
cost = cost - .25;
cout << "Remaining balance:\t" << cost << endl;
break;
case 'o':
cout << "You insert one dollar into the vending machine.\n";
cost = cost - 1.00;
cout << "Remaining balance:\t" << cost << endl;
}
}while ( cost > 0);
}
void vendingprocess()
{
if ( cost < 0 )
{
change = -1 *cost;
cout << "Your change is :\t"<<change<<endl;
cout << "Please take your drink!\n";
}
}
int main()
{
vendingmachine();
vendingprocess();
system("pause"); // I'm using system pause because cin.get() exits the program right away!
return EXIT_SUCCESS;
}
Thanks,
Derek