Well the prof describes it as a vending machine program but there are no menus, the user just enter an amount that is multiple of 5 and i'm supposed to display the change due in terms of quarters, dimes, etc. The problem i have is that the machine is initially filled with 2 quarters, 4 dimes, and 4 nickels, and once there are no more quarters i should be able to give back change in dimes and nickels as long as there is enough change for it and when i tried test cases after no quarters were left it will give more quarters as change. I will appreciate if someone points out what I'm doing wrong and here is my current code(lil messy, trying to make it work before clean up).
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int const QUARTER = 25;
int const NICKEL = 5;
int const DIME = 10;
int totalQuarters, totalNickels, totalDimes,q,n,d;
int currentBalance,amount, change,change2,remainder,changeleft;
double totalBalance;
ifstream coinIn;
cout<<"Vending Machine\n\n";
cout<<"Filling machine with change. Please wait....\n";
coinIn.open("coin.dat");
coinIn >> totalQuarters>>totalDimes>>totalNickels;
cout<<"There are "<<totalQuarters<<" quaters, "<<totalDimes<<" Dimes,and "<<totalNickels<<" Nickels."<<endl;
currentBalance = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
cout<<fixed<<setprecision(2)<<showpoint;
cout<<"Initial machine balance is $"<<currentBalance/100.00<<endl;
cout<<"Enter a purchase amount [0-100] -->";
cin>>amount;
changeleft = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
if (amount >= 0 && amount <= 100)
{
while (amount != 0)
{
change = 100-amount;
cout<<"\n\nYou entered a purchase amount of "<<amount<<" cents."<<endl;
cout<<"Please enter one-dollar bill"<<endl;
cout<<"Processing your purchase"<<endl;
cout<<"Thank you for your purchase"<<endl;
change2 = change;
if( totalQuarters != 0 && change2 < changeleft)
{
q = change2/QUARTER;
remainder = change2%QUARTER;
totalQuarters -= q;
d = remainder / DIME;
remainder = remainder%DIME;
totalDimes -= d;
n = remainder / NICKEL;
remainder = remainder%NICKEL;
totalNickels -=n;
}
else if (totalDimes != 0 && change2 < changeleft)
{
d = change2/ DIME;
remainder = remainder%DIME;
totalDimes -= d;
n = remainder / NICKEL;
remainder = remainder%NICKEL;
totalNickels -=n;
}
else if (totalNickels != 0 && change2 < changeleft)
{
n = change2 / NICKEL;
remainder = remainder%NICKEL;
totalNickels -=n;
}
else
{
cout<<"Insufficient change!\n";
cout<<"Your transaction cannot be processed.\n";
cout<<"Please take back your dollar bill.\n";
cout<<"\n\nEnter a purchase amount [0-100] -->";
cin>>amount;
}
changeleft = (QUARTER * totalQuarters) + (NICKEL * totalNickels) + (DIME * totalDimes);
totalBalance = (changeleft + amount)/100.00;
cout<<"Your change of "<<change<<" is given as:"<<endl;
cout<<q<<" quaters, "<<d<<" Dimes, and "<<n<<" Nickels ("<<change<<" cents.)"<<endl;
cout<<"\n\nEnter a purchase amount [0-100] -->";
cin>>amount;
}
}
else
{
cout<<"Invalid amount entered, please enter a valid amount\n";
}
cout<<"Output file is being generated....\n";
cout<<"Machine is shutting down. Good bye.\n";
ofstream balance;
balance.open("machine.txt");
balance<<"Number of quarters: "<<totalQuarters<<endl;
balance<<"Number of dimes: "<<totalDimes<<endl;
balance<<"Number of nickels: "<<totalNickels<<endl;
balance<<fixed<<setprecision(2)<<showpoint;
balance<<"Current balance: $"<<totalBalance<<endl;
coinIn.close();
return 0;
}
In some cases the result is correct like the attachment screen shows, but in many others i get quarters when I was not supposed to.