Convesion of cents to dollars.

Am trying to see how I can get the dollar into my code to see $ D. CC. in the code that I have already made, when I put 2 dimes, 3 shillings, 3 nickels, and o pennies. I will get $2.2 but am interested in saying $2.20. this project is for my university assignment that is the laboratory report the program is also to tell the user the number of cents they have in their pockets after asking them the change that they have. https://www.theengineeringprojects.com/2019/11/introduction-to-properties-in-c.html
This program is about the input/output concept that is normally applied in the calculator. If you are possible to tell the user the amount of the cents that they possess, also make sure that you tell them the amount of dollars that they have. This has to involve the use of the real numbers and the currency being used is in the form $ D.CC.
Dollars might have multiple columns but for the cent, there can only be two columns. The class C ++ will always have a sample code for us to use:


#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;
}
Last edited on
#include <iomanip> and then

1
2
3
4
5
const int cents = quarters*25 + dimes*10 + nickels*5 + pennies ;
const double dollars = cents / 100.0 ;
std::cout << "you have " << cents << "cents\nie. $" 
              << std::fixed << std::setprecision(2) // fixed format with two digits after the decimal
              << dollars << '\n' ;


Topic archived. No new replies allowed.