okay so i understand modulus
i understand that you can only use int when working with modulus
i understand that it calculates the remainder
the program im writing prompts the user for purchase and payment amount, then calculates the amount of dollars, half-dollars, quarters, nickles, dimes in change.
im completely stuck on the modulus part.
i can't figure out what to declare as a int or a double.
i know i can't have the variables listed under as const doubles as int, since that would make any sense.
i know change has to be in double,
but don't i have to use change in the mod calculations?
maybe the answers obvious, but i'm not getting it.
a little help???
here is a better description of what im trying to accomplish
1.
Prompt the user for the amount of the purchase (in cents.)
Read user input.
2.
Prompt the user for the amount received in payment (in cents.)
Read user input.
3.
Calculate the change. (in cents)
4.
Calculate the number of dollar bills to be given in change. (Use the
division operator.)
*
Calculate the remaining change (Use the mod operator.)
5.
Calculate the number of half-dollars to be given in change. (Use the
division operator.)
*
Calculate the remaining change (Use the mod operator.)
6.
Repeat the process for the remaining coins/change.
7.
Display the author’s identifying information to the output file.
8.
Display the results to the output file.
9.
Display the author’s identifying information on the screen.
10.
Display an appropriate message on the screen indicating to the user the
name of the output file to which the results have been written.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ( )
{
const double DOLLARS = 1.0,
HALF_DOLLARS = 0.5,
QUARTERS = 0.25,
DIMES = 0.1,
NICKELS = 0.05,
PENNIES = 0.01;
double change,
payment_amount,
purchase_amount;
int amount_dollars,
amount_half_dollars,
amount_quarters,
amount_dimes,
amount_nickels,
amount_pennies;
cout << "Enter amount of the purchase (in cents): ";
cin >> purchase_amount;
cout << "Enter amount received in payment (in cents): ";
cin >> payment_amount;
change = payment_amount - purchase_amount;
amount_dollars = change / DOLLARS;
// modulus calc here for remainder
amount_half_dollars = change / HALF_DOLLARS;
// modulus calc here for remainder
amount_quarters = change / QUARTERS;
// modulus calc here for remainder
amount_dimes = change / DIMES;
// modulus calc here for remainder
amount_nickels = change / NICKELS;
// modulus calc here for remainder
amount_pennies = change / PENNIES;
// modulus calc here for remainder
ofstream outfile;
outfile.open ("file_name.txt");
outfile << fixed << setprecision(2)
<< endl << endl << "Received " << payment_amount <<
<< " for a purchase of " <<
<< purchase_amount << endl
<< endl << endl << "Change in coins:" << endl
<< "Coin " << setw(6) << "Number" << endl
<< "_____________________" << endl
<< "Change " << setw(6) << change << endl
<< "Dollars " << setw(6) << amount_dollars << endl
<< "Half-Dollars " << setw(6) << amount_half_dollars << endl
<< "Quarters " << setw(6) << amount_quarters << endl
<< "Dimes " << setw(6) << amount_dimes << endl
<< "Nickels " << setw(6) << amount_nickels << endl
<< "Pennies " << setw(6) << amount_pennies << endl <<
<< endl;
cout << "Program results will be written to file_name.txt.";
outfile.close();
return 0;
}
|
rough example of
output file results:
(ignore any setw errors or w/e i totally just wrote this out myself)
Recieved 40.00 for a purchase of 34.01
Change in coins:
Coins Number
______________________
Dollars 5
Half Dollars 1
Quarters 1
Dimes 1
Nickels 0
Pennies 4 |