-1

The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.

#include <iostream>
using namespace std;

int main()
{
    int quarter, dime, nickle;
    int result;

    quarter = 25;
    dime = 10;
    nickle = 5;

    int numQuarters, numDimes, numNickles;

    cout << "Please enter the number of quarters and press Enter: ";
    cin >> numQuarters;

    cout << "Please enter the number of dimes and press Enter: ";
    cin >> numDimes;

    cout << "Please enter the number of nickles and press Enter: ";
    cin >> numNickles;

    result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);

    cout << "The total amount of pennies is: " << result;

    return 0;
}

I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies

The output is 102

Edit: Code Fixed and working now!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Perhaps you need to multiply everything by 100, not just dimes. Are parentheses missing? – user7860670 Sep 15 '19 at 22:47
  • 1
    Also, the values of `nickle` and `dime` seem to be interchanged. – Pete Becker Sep 15 '19 at 22:49
  • Once you’ve sorted out the typos, you’ll find that floating point values are not well suited to currency calculations. .1 and .05 can’t be represented exactly as floating-point values (unless your hardware uses decimal floating-point, which it doesn’t). Do your calculations in pennies instead. A dime is 10, a nickel 5, and a quarter 25. – Pete Becker Sep 15 '19 at 22:54
  • Thank you very much but the problem doesn't seem to be fixed I will reformat again. I will edit with your fix and update the post. Thank you! – 策略玩家Gusion Sep 15 '19 at 22:57
  • Oh, just a quick question for Pete and VTT. Why is this marked duplicate? The question you linked only solves a portion of Gusion's problem, and it doesn't tell him why or how to fix his code. Just curious, that's all. –  Sep 15 '19 at 23:01
  • @Gusion Just a tip from me, I always change the conversion rates at the end, for example if I'm dealing with money, I use whole numbers, but when I actually print the result, I change it to the proper format. Just keeps things nice and clean. –  Sep 15 '19 at 23:03
  • @FuzzySquid Thanks for your suggestion! will keep it in mind! – 策略玩家Gusion Sep 15 '19 at 23:07

1 Answers1

0

Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...

#include <iostream>
using namespace std;

int main()
{
    float quarter, dime, nickle, penny;
    float result;

    quarter = 0.25;
    dime = 0.10;
    nickle = 0.05;

    float numQuarters, numDimes, numNickles;

    cout << "Please enter the number of quarters and press Enter: "; 
    cin >> numQuarters;

    cout << "Please enter the number of nickles and press Enter: ";
    cin >> numNickles;

    cout << "Please enter the number of dimes and press Enter: ";
    cin >> numDimes;

    result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);

    cout << "The total amount of pennies is: " << result * 100;

    return 0;
}

Oh, and just like what @Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Thanks everyone who answered! I fixed it with your suggestions of changing values of pennies, quarters and dimes to whole numbers and changed the math portion! I'm new to coding and I learned a new lesson! Thank you all! – 策略玩家Gusion Sep 15 '19 at 23:03