Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> using namespace std; int main() { //declare variables double

ID: 3799645 • Letter: #

Question

#include <iostream>

using namespace std;

int main()
{
    //declare variables
    double dollarAmount;
    int quarters,
        dimes,
        nickels,
        pennies,
        totalCents;

//Prompt the user to enter the amount of money they have before the conversion
cout << "Due to the Martian Money Weevel plague, we need to turn your money into all coins. ";
cout << "Please enter the amount of money you have so that you don't break the law. $";
cin >> dollarAmount;

//turn the dollar amount into cents
totalCents = dollarAmount * 100;
//calculate and store the amount of full quarters in the amount
quarters = totalCents / 25;
//calculate and store the remaining change
totalCents = totalCents % 25;
//calculate and store the amount of full dimes in the remaining change
dimes = totalCents / 10;
//calculate and store the remaining change
totalCents = totalCents %10;
//calculate and store the amount of full nickels in the remaining change
nickels = totalCents / 5;
//calculate and store the remaining amount into pennies
pennies = totalCents%5;

//Print the amount of each type of coin to give the smallest amount of change
cout << "Alright this is the least amount of change I can give you: ";
cout << quarters << " Quarter(s) ";
cout << dimes << " Dime(s) ";
cout << nickels << " Nickel(s) ";
cout << pennies << " Pennie(s)";

return 0;
}

/*
Sample run
Due to the Martian Money Weevel plague, we need to turn your money into all coins.
Please enter the amount of money you have so that you don't break the law.
$123.44
Alright this is the least amount of change I can give you:
493 Quarter(s)
1 Dime(s)
1 Nickel(s)
3 Pennie(s)
Process returned 0 (0x0)   execution time : 2.330 s
Press any key to continue.
*/

I can't figure out whats wrong with my program. It keeps rounding incorrecltly for the pennies.

Explanation / Answer

Hi

I dont see any issue with that code. it is working fine.

#include <iostream>
using namespace std;
int main()
{
//declare variables
double dollarAmount;
int quarters,
dimes,
nickels,
pennies,
totalCents;
//Prompt the user to enter the amount of money they have before the conversion
cout << "Due to the Martian Money Weevel plague, we need to turn your money into all coins. ";
cout << "Please enter the amount of money you have so that you don't break the law. $";
cin >> dollarAmount;
//turn the dollar amount into cents
totalCents = dollarAmount * 100;
//calculate and store the amount of full quarters in the amount
quarters = totalCents / 25;
//calculate and store the remaining change
totalCents = totalCents % 25;
//calculate and store the amount of full dimes in the remaining change
dimes = totalCents / 10;
//calculate and store the remaining change
totalCents = totalCents %10;
//calculate and store the amount of full nickels in the remaining change
nickels = totalCents / 5;
//calculate and store the remaining amount into pennies
pennies = totalCents%5;
//Print the amount of each type of coin to give the smallest amount of change
cout << "Alright this is the least amount of change I can give you: ";
cout << quarters << " Quarter(s) ";
cout << dimes << " Dime(s) ";
cout << nickels << " Nickel(s) ";
cout << pennies << " Pennie(s)";
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                                           

Due to the Martian Money Weevel plague, we need to turn your money into all coins.                                                                                                                                                                                     

Please enter the amount of money you have so that you don't break the law.                                                                                                                                                                                             

$123.44                                                                                                                                                                                                                                                                

Alright this is the least amount of change I can give you:                                                                                                                                                                                                             

493 Quarter(s)                                                                                                                                                                                                                                                         

1 Dime(s)                                                                                                                                                                                                                                                              

1 Nickel(s)                                                                                                                                                                                                                                                            

4 Pennie(s)