You are here:

# C++/coins

Question
I need help with this program I thought I could at least get it started but it just is not making any sense to me.  I don't even know how I can show the value in dollars and cents.  Any thoughts, any help would be great.  I'm just feeling so dumb.

A bank teller needs a program to total the amount of money in his coin tray.  Write a program that asks the user to enter the number of each type of U.S. coin (half dollars, quarters, nickels, dimes, and pennies) and that displays the total value of the coins in dollars and cents.

Hello Jeffrey, thanks for the question.

Perhaps you are overthinking the problem here. All you really need to do to give the impression of dollars and cents is to total everything up and store it in a float:

#include <iostream>
using namespace std;

#define HDVALUE .50f
#define  QVALUE .25f
#define  DVALUE .10f
#define  NVALUE .05f
#define  PVALUE .01f

int main()
{
int hd, q, d, n, p;
float total;

cout << "Half Dollars: ";
cin >> hd;

cout << "\nQuarters: ";
cin >> q;

cout << "\nDimes: ";
cin >> d;

cout << "\nNickels :";
cin >> n;

cout << "\nPennies :";
cin >> p;

total = hd * HDVALUE + q * QVALUE + d * DVALUE +
n *  NVALUE + p * PVALUE;

cout << "\nThe total is " << total << '\n';

return 0;
}

As you see, that totals everything up and prints out the correct dollar and change amount. You don't have to #define all the values if you want, you could use a const float instead, but I like #define because it is a simple text replacement as opposed to the 4 bytes of memory a piece that the const float's would require.

I hope this information was helpful.

- Eddie

C++

Volunteer

#### Eddie

##### Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

##### Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.