Also, this is not C++ but ANSI C, hope it helps
Main Topics
Browse All TopicsThis question was asked previously but, to no avail.
I am using code blocks as my compiler.
I have a program that I am trying to figure out.
The instructions given is:
Write a C++ program will take in the price of an item purchased and the amount of cash tendered. The program will output the amount of change and then break this down
into dollars, quarters, dimes, nickels and pennies.Use the modulus operator. You may be off by one penny, but see if
you can find a way to fix that by writing code to perform rounding. When you assign a double to an int, the decimal
portion is truncated.
Example run:
Enter the price of the item: 3.35
Enter the amount of cash tendered: 5.00
Your change is $1.65
1 dollar
2 quarters
1 dimes
1 nickel
0 pennies
Please help me figure out how to implement the mod operator and get the correct change.
I asked this question previously and the expert didn't give answer using double.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok,
5.00 - 3.35 = 1.65
Dollars = 1
Quarters = 2
Cents = 15
Doing some math, you have 65 cents if I am right. So, 2 Quarters are 50 cents plus the remaining 15 cents equals .65 which is the decimal part that you have.
If you need C++ only, then change the printf and scanf functions I used and change them for cin and cout you had previously.
Okay, that's cool. Thank you!
To help, I have found other code that may be helpful but it doesn't use just the iostream
header library. It also uses another.
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// State Variables.
int dollars ;
int quarters ;
int dimes ;
int nickels ;
int pennies ;
double itemPrice ;
double cashTendered ;
// State the price of the item.
cout << "What was the price of the item purchased?" ;
cin >> itemPrice ;
// Get the money recieved from the person.
cout << "How much did you hand the cashier?" ;
cin >> cashTendered
// Conversion from cash to pennies.
pennies = ceil(((cashTendered - itemPrice)*100)) ;
// Dollars if needed (if any).
dollars = pennies / 100 ;
// Find how many pennies remain.
pennies = pennies % 100 ;
// Quarters if needed (if any).
quarters = pennies / 25;
// Find how many pennies remain.
pennies = pennies % 25;
// Dimes if needed (if any).
dimes = pennies / 10;
// Find how many pennies remain.
pennies = pennies % 10;
// Nickels if needed (if any).
nickels = pennies / 5;
// Find how many pennies remain.
pennies = pennies % 5;
// Result Output
cout << "Your change back is " << (cashTendered - itemPrice) << endl;
cout << dollars << " dollars" << endl;
cout << quarters << " quarters" << endl;
cout << dimes << " dimes" << endl;
cout << nickels << " nickels" << endl;
cout << pennies << " pennies" << endl;
}
This does not compile correctly though giving an error
C:\Documents and Settings\Seth\Desktop\main
I am not sure.
Final Code (I realize that the number is off 1, but that's due to scenario). The code above works perfectly (that I pasted, just put a semicolon after cashTendered).
::::::::::
#include <iostream>
using namespace std;
int main ()
{
// State Variables.
int dollars ;
int quarters ;
int dimes ;
int nickels ;
int pennies ;
double itemPrice ;
double cashTendered ;
// State the price of the item.
cout << "What was the price of the item purchased?" ;
cin >> itemPrice ;
// Get the money recieved from the person.
cout << "How much did you hand the cashier?" ;
cin >> cashTendered ;
// Conversion from cash to pennies.
pennies = (cashTendered - itemPrice)*100 ; ;
// Dollars if needed (if any).
dollars = pennies / 100 ;
// Find how many pennies remain.
pennies = pennies % 100 ;
// Quarters if needed (if any).
quarters = pennies / 25;
// Find how many pennies remain.
pennies = pennies % 25;
// Dimes if needed (if any).
dimes = pennies / 10;
// Find how many pennies remain.
pennies = pennies % 10;
// Nickels if needed (if any).
nickels = pennies / 5;
// Find how many pennies remain.
pennies = pennies % 5;
// Result Output
cout << "Your change back is " << (cashTendered - itemPrice) << endl;
cout << dollars << " dollars" << endl;
cout << quarters << " quarters" << endl;
cout << dimes << " dimes" << endl;
cout << nickels << " nickels" << endl;
cout << pennies << " pennies" << endl;
}
Business Accounts
Answer for Membership
by: _phoenix_Posted on 2009-09-19 at 00:15:20ID: 25371856
Try this my friend :) before giving points.
Select allOpen in new window