-7

THE CODE IS NOT WORKING PROPERLY !! I am newbee to C++ and my assignment to write a code that takes a value from the user ( amount of money ) and then convert it into " quarters = 25 cents , dimes = 10 cents , nickels = 5 cents and pennies = 1 cent " so for example when i enter the value 7.47 i should get 29 quarter , 2 dimes , 0 nickels , 2 pennies and so on ... my problem is that i have tried many values and it work just fine , but when i tried the value 9.53 i should get 38 quarters , 0 dimes , 0 nickels and 3 pennies BUT instead i get 38 quarters , 0 dimes , 0 nickels and 2 pennies the same error happens when i try 8.53 , but when i try 6.53 ,5.53 .4.53 it works well !! i am so confused now , so please help !!

`#include<iostream>
using namespace std;
int main()
{
    double money, c_money, quarters, dimes, nickels, pennies, remainder; char response; 
    new_input:                                      
    cout << " Enter the amount of money to be converted : " << endl;
    cin >> money;
    while (money < 0)
    {                                                                               
        cout << " Invalid input , please enter a non-negative value " << endl;      
        cin >> money;
    }
    c_money = money * 100;                                              
    quarters = (int)c_money / 25;
    remainder = (int)c_money % 25;
    dimes = (int)remainder / 10;
    remainder = (int)remainder % 10;
    nickels = (int)remainder /5;
    remainder = (int)remainder % 5;
    pennies = (int)remainder ;
    cout << endl;
    cout << " The amount of money entered could be represented as : " << endl;
    cout << "*****************************************************" << endl;
    cout <<" Number of quarters : "<< quarters << endl;
    cout <<" Number of dimes    : "<<dimes << endl;
    cout <<" Number of nickels  : "<< nickels << endl;
    cout <<" Number of pennies  : "<< pennies << endl<<endl;
    cout << "Do you want to enter more values ?? type , y or n and press Enter ! " << endl;    
    cin >> response;
    if (response == 'y')
    {
        goto new_input;
    }
    else { cout << " Thanks for using our app !! " << endl << endl; }
    return 0;
}` 
Sayed Alesawy
  • 425
  • 2
  • 6
  • 18

1 Answers1

3

You are a victim of double inaccuracy. Your code may work in 99% cases, but the inaccuracy comes in the remaining 1%...

I suggest you don't use double when you need to make exact calculations, e.g. concerning money where every penny counts. You will be better of by replacing it with int, multiplying it by 100 (so you don't lose the decimal part) and do all calculations with integers.

radoh
  • 4,554
  • 5
  • 30
  • 45
  • The assignment is considering the input to be something like this 9.75 , the user will enter like that , with the decimal part , so if turned everything to int instead of double , there will be data loss from the very beginning on the code , user enters 9.75 , it will be implicitly converted into 9 causing loss of 0.75 – Sayed Alesawy Feb 06 '16 at 20:14
  • No, you didn't read properly what I wrote. You multiply it by 100, so you get 975. – radoh Feb 06 '16 at 20:16
  • I did read it , but i need to get the value at first and then i multiply it !! , can you write some code for your proposal , maybe i am still getting you wrong – Sayed Alesawy Feb 06 '16 at 20:21
  • I was just trying to give a general idea how to approach the problem. Of course, when you get the value from user, you can use double, but when you start doing any calculations, multiply it by 100 and convert to int. – radoh Feb 06 '16 at 20:32
  • okay , that is what i did – Sayed Alesawy Feb 07 '16 at 07:21