Computer Programming Project

Write your question here.
Can anyone help evaluate any errors with the code I wrote?
The program is suppose to calculate the user's money into the minimum number of U.S. coins.
When I began to build the solution it showed 5 uninitialized local variables and conversion from double to int "possible loss of data". Don't really know how to address and resolve the issue. Any suggestions?

#include <iostream>
#include <math.h>
#include <iomanip>


using namespace std;

int main() {

int m_total;
double money, coin_q, coin_d, coin_n, coin_p, remain1, remain2, remain3, remain4;


m_total = ceil(money*100);

cout << "Welcome to the Coin Exchange system.";

cout << "Please enter your money amount (ie. $14.44):";
cin >> money;

if (remain1 == m_total % 25) {
coin_q = (m_total - remain1) / 25;
}

if (remain2 == m_total % 10) {
coin_d = (m_total - remain2) - (coin_q * 25) / 10;
}

if (remain3 == m_total % 5) {
coin_n = (m_total - remain3) - (coin_q * 25) - (coin_d * 10) / 5;
}

if (remain4 == (m_total % 1)) {
coin_p = (m_total - remain4) - (coin_q * 25) - (coin_d * 10) - (coin_n * 5) / 1;
}

cout << "Number of Quarters:" << coin_q << endl;
cout << "Number of Dimes:" << coin_d << endl;
cout << "Number of Nickels:" << coin_n << endl;
cout << "Number of Pennies:" << coin_p << endl;

system("Pause");
return 0;
}

Last edited on
A lot of questions for this problem have been asked recently on the forum - try using the search feature or google search. If you still need help then let us know.
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
#include <iostream>

using namespace std;

int main() {

double money;
int coin_q = 0, coin_d = 0, coin_n = 0, coin_p = 0;

cout << "Welcome to the Coin Exchange system.";

cout << "Please enter your money amount (ie. $14.44):";
cin >> money;

if(money<0){ 
  coin_q = (money + .25) / .25 * .25;
  coin_d = ((money - coin_q ) + .10) / .10 * .10;
  coin_n = ((money - coin_q - coin_d ) + .5) / .5 * .5;
  coin_p = ((money - coin_q - coin_d - coin_n ) + .1) / .1 * .1;
} 
if(money!=0)
{
cout << "\nNumber of Quarters:" << coin_q << endl;
cout << "Number of Dimes:" << coin_d << endl;
cout << "Number of Nickels:" << coin_n << endl;
cout << "Number of Pennies:" << coin_p << endl;
}else{cout<<"No money to exchange.\n";}

return 0;
}

Not so sure if the rounding will work as I think...
The program works, but the output/results all show zeros when I entered 1.16 as the input.
You should never use floating point types for money/currency. Always use whole numbers to represent the number of cents.
Instead of fiddling with floating point IO, I simply read the total into a string and then removed the decimal point character and finished by converting the string directly into an int. Heres the function, it took like 2 minutes to write and its important to know how to convert strings.
1
2
3
4
5
6
7
8
9
10
11
12
int strDecToInt(string s)
{
    for (int i = 0; i < s.size(); i++)
        if (s[i] == '.')
        {
            s = s.substr(0, i) + s.substr(i+1);
            break;
        }
    stringstream ss(s);
    int n; ss >> n;
    return n;
}


I trust that you can work through the above code and extend it into your program; ask away if you have any questions. It might be too complex for what you need, but I find it works pretty well by avoiding IO issues and type conversions.
Thanks for all the help guys, really appreciate it!
Topic archived. No new replies allowed.