# Thread: Simple Program Counting Coins. What is Wrong With My Logic. Never Programmed Before.

1. ## Simple Program Counting Coins. What is Wrong With My Logic. Never Programmed Before.

Hi Everyone -

This is my first post here, and I made sure to use the search utility before posting this.

Today, we were given our first programming assignement on the first day of class. The class is intro to programming, and we are using the C++ language which apparently is the best out there.

Well, we were given a program to count coins and from the limited knowledge I have with C++ ( I never progammed before until today ) I am doing everything correctly except for where I am computing the value of my coins.

Could anyone give me a clue on...
1) what I am doing wrong?
2) and how to convert my total value of money into a dollar amount such as if I had 8 quarterrs and 12 pennies it would display \$2.12

Thanks for any assistance provided.

Code:
```#include <iostream>
using namespace std;

int main()
{
// used int data type because you can never have a fraction of a coin , ideally
int quarters, dimes, nickles, pennies;
quarters = dimes = nickles = pennies = 0;

cout << "Enter the number of quarters:";
cin  >> quarters;
cout << "Enter the number of dimes   :";
cin  >> dimes   ;
cout << "Enter the number of nickles :";
cin  >> nickles ;
cout << "Enter the number of pennies :";
cin  >> pennies ;

cout << quarters << " " << dimes << " " << nickles << " " << pennies << " " << endl << endl << endl << endl;

float money = 0.0 // amount of money i have
money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);

cout << "you have this amount        :" << money << endl;

return 0;
}```

2. There are two mistakes
First, you forgot the semicolon at the end of
Code:
`float money = 0.0 // amount of money i have`
Second
Code:
`money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);`
Code:
`money = (quarters * .25) + (dimes * .10) + (nickles * .05) + (pennies * .01);`

3. WoW!

Thanks for the prompt reply. I am amazed.

Code:
`float money = 0.0;`
but unsure what happend to it when I pasted in my code.

I now see my mistake. I feel so silly and dumb for making it.

Thank you.

4. First off, dont use more than 1 endl ever. Use "\n" if you want to add a verticle space, but dont use several endl's because it becomes useless to flush the buffer so many times.

Ok, first:

Code:
```	float money = 0.0 // amount of money i have
money = (quarters * .25) + (dimes * .10) + (nickles * .5) + (pennies * .1);```
This is bad. You are attempting to turn an int into a float. (Why are you using a float? Use a double, its more accurate.) You have to cast an int into a float before you can use it in equasions with floating numbers! The simplest way to fix this is to convert all your ints to floats (or doubles). Also, the way you use cin >> to get number input is very bad. If the user types in a letter, cin wont understand it.

Code:
```#include <iostream>
using namespace std;

int main() {
double quarters, dimes, nickles, pennies;

cout << "Enter the number of quarters:";
cin  >> quarters;
cout << "\nEnter the number of dimes   :";
cin  >> dimes   ;
cout << "\nEnter the number of nickles :";
cin  >> nickles ;
cout << "\nEnter the number of pennies :";
cin  >> pennies ;

cout << quarters << " " << dimes << " " << nickles << " " << pennies << " " << endl;

float money = 0.0; // amount of money i have
money = (quarters * .25) + (dimes * .10) + (nickles * .05) + (pennies * .01);

cout << "you have this amount        : \$" << money << endl;

return 0;
}```
That should work, I know you cant have a fraction of a coin, but you have to cast int's to float's before you can multiply them by decimals, I would tell you how, but I dont remember -,-.

5. >> You have to cast an int into a float before you can use it in equasions with floating numbers!

No, you don't have to. When you are dividing ints and you want a floating point result, you should cast one of them to a double first, but in this case it isn't necessary.

6. > Why are you using a float? Use a double, its more accurate.
Better yet, dont use any floating point for money at all.

Use an integer to represent the number of cents, and use /100 and %100 to extract the \$ and cent amounts.