Converting cents to dollar with $D.CC form where dollar can have many columns but cents have to have 2

I'm trying to get the the dollar to say $D.CC. On my code when you put 8 quarters, 2 dimes, 0 nickels, 0 pennies you get get $2.2 but I want it to say $2.20. It is for a school assignment this is the lab

Ask the user what change they have in their pocket, and then tell them the total number of cents they have. Develop your algorithm first, and then write your code. Both are to be turned in.

This lab brings together the concepts of input and output in the practice of a calculator. Once you are able to tell the user the amount in cents that they have, also tell them how much that is in dollars. For this you will need to use “real” numbers, and remember that our currency is expressed in this fashion:
$D.CC
Where there may be multiple columns for dollars but there are always two for cents. The C++ Book (link in Classroom Basics) will have refresher examples of formatting output.




#include <iostream>


using namespace std;

int main()
{
//make variables
int quarters, dimes, nickels, pennies;

//gather data from user
cout << "We're going to find out how much money you have in change.\n";
cout << "how many qurters do you have?\n";
cin >> quarters;
cout << "How many dimes do you have?\n";
cin >> dimes;
cout << "How many nickels do you have?\n";
cin >> nickels;
cout << "How many pennies do you have?\n";
cin >> pennies;

//clear the screen
system("cls");

//display final output
cout << "You have " << quarters*25 + dimes*10 + nickels*5 + pennies*1 << " cents\n";
cout << "You have "<<"$"<< quarters*.250 + dimes * .100 + nickels * .050 + pennies * .010 << "\n";

return 0;
}
welcome! please use code tags next time.
consider:
double f = 123.4567;
cout.precision(2);
cout << fixed;
cout << setfill('0');
cout << f << endl;

edit.. bad example, but try printing 10.0 too, youll see.. :)
Last edited on
it just shows $10
No it doesn't?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    {
    double f = 10.0;
    cout.precision(2);
    cout << fixed;
    cout << setfill('0');
    cout << f << endl;
    }
    {
    double f = 2.2;
    cout.precision(2);
    cout << fixed;
    cout << setfill('0');
    cout << f << endl;
    }
}

10.00
2.20


In a real-life program, you would avoid any floating-point calculations with money in the first place, and just put a decimal point between the third-to-last and second-to-last digit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <string>

// no form of rounding is done
std::string to_money(int cents)
{
    std::string money = std::to_string(cents / 100) + ".";
    if (cents % 100 < 10)
        money += "0"; // alternatively, could use stream manipulators (setfill)
    money += std::to_string(cents % 100);
    
    return money; 
}

int main()
{
    std::cout << to_money(0) << '\n';
    std::cout << to_money(5) << '\n';
    std::cout << to_money(220) << '\n';
    std::cout << to_money(1000) << '\n';
}

0.00
0.05
2.20
10.00
Last edited on
the formatters stick until you change one, don't need to repeat them. (I know you know that and just pasted Ganado, saying it for OP).
Topic archived. No new replies allowed.