C ++,获取大量资金以转换为四分之一,一角硬币,五分之一硬币,几美分硬币的代码 [英] C++ , A code to get an amount of money to convert into quarters, dimes , nickels, pennies

查看:114
本文介绍了C ++,获取大量资金以转换为四分之一,一角硬币,五分之一硬币,几美分硬币的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码无法正常工作!!
我是C ++的新手,我的任务是编写一个从用户那里获得一个值(金额)的代码,然后将其转换为季度= 25美分,1美分= 10美分,镍= 5美分和美分= 1 cent
因此,例如,当我输入值7.47时,我应该得到29个季度,2个角钱,0个镍币,2个便士,依此类推...
我的问题是我尝试了很多值并且它工作得很好,但是当我尝试值9.53时,我应该得到38个季度,0个角钱,0个镍币和3个便士,但我却得到了38个季度,0个角子,0个镍币和2个便士
同样的错误发生当我尝试8.53时,但是当我尝试6.53,5.53 .4.53时,效果很好!我现在很困惑,所以请帮忙!

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;
}` 


推荐答案

您是受害者 double 错误。您的代码可能在99%的情况下都能正常工作,但其余的1%会导致不准确...

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

我建议您不要使用 double 当您需要进行精确计算时,例如关于金钱,每一分钱都很重要。最好用 int 替换它,再乘以100(这样就不会丢失小数部分),并使用整数进行所有计算。

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.

这篇关于C ++,获取大量资金以转换为四分之一,一角硬币,五分之一硬币,几美分硬币的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆