Can't figure out error in C++ when trying to convert quarter, dimes, nickels, and pennies into just dollars and pennies

Viewed 42

I'm taking an online C++ course online and came across this problem:

Given four values representing counts of quarters, dimes, nickels and pennies, output the total amount as dollars and cents.

Output each floating-point value with two digits after the decimal point, which can be achieved by executing cout << fixed << setprecision(2); once before all other cout statements.

I have written the following code and checked it putting in various different inputs. I put in "4 3 2 1" for quarters, dimes, nickels, and pennies respectively. My program outputted "Amount: $1.41" But when I input "0 0 1 1" my program outputs "Amount: $0.6" when it should be "Amount: $0.06" Unfortunately there is no answer that I can look at or no hints. I've been trying to solve this problem for awhile with no success. Would someone tell me what I am doing wrong?

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   double dollars;
   int quarters;
   int dimes;
   int nickels;
   int pennies;
   
   /* Type your code here. */
   cin >> quarters;
   cin >> dimes;
   cin >> nickels;
   cin >> pennies;
   
   dollars = ((quarters * 25) + (dimes * 10) + (nickels * 5) + pennies) / 100;
   pennies = ((quarters * 25) + (dimes * 10) + (nickels * 5) + pennies) % 100;
   cout << fixed << setprecision(2);
   cout << "Amount: $" << static_cast<int>(dollars) << "." << pennies << endl;
   return 0;
}
0 Answers
Related