i know this is probably something really simple, but i just cant figure it out. here is the code. i know i should be able to calculate it without using the newchange variable. any help is appreciated.
Code:
#include <iostream>
using namespace std;
void main()
{
// Programmer: Mike Gozdiskowski
// Date: 9/13/2002
// Discussion: An interactive program to calculate the change
// from a vending machine
// Input: Cost of item purchased (an integer between 0 and 100 inclusive)
// Output: Change is quarters, dimes, nickels, and cents
// Declaration and description of variables
long cost; // Cost of item purchased in cents
long change; // Amount of change in cents
long quarters; // Number of quarters in change
long dimes; //Number of dimes in change
long nickels; //Number of nickels in change
long pennies; //Number of pennies in change
long newchange; //Variable used to store temporary amount of change for each coin
// Welcome user and get amount of purchase
cout<<"Welcome to the cash register!"<<endl;
cout<<"Please enter the amount of your purchase: ";
cin>>cost;
// Calculate the total change due
change=(100-cost);
// Calculate the number of quarters, dimes, and nickels in change
quarters=change/25;
newchange=change-quarters*25;
dimes=newchange/10;
newchange=newchange-dimes*10;
nickels=newchange/5;
newchange=newchange-nickels*5;
pennies=newchange/1;
// Print the results and exit program
cout<<"Total change due: "<<change<<" cents"<<endl;
cout<<"Your change is"<<endl;
cout<<" "<<quarters<< "quarter(s)"<<endl;
cout<<" "<<dimes<< "dime(s)"<<endl;
cout<<" "<<nickels<< "nickel(s)"<<endl;
cout<<" "<<pennies<< "pennie(s)"<<endl;
}
i did try something like this originally but i could not get it to work right so i just created a new variable. it would put the correct output only for some numbers.
Code:
// Calculate the number of quarters, dimes, and nickels in change
quarters=change/25;
dimes=(change%25)/10;
nickels=(change%10)/5;
pennies=(change%5)/1;