我的代码中有一些我无法找到的错误

时间:2014-09-15 23:09:53

标签: c++ debugging

我正在制作一个名为" The Vending Machine"在我的C ++类中,一切正常,除了一个输出,当我使用Dollar + Quarter + Quarter时,应该是1.50美元然后循环应该死掉但是它不会。所以我希望也许有人在这里看到我无法看到的东西。该程序的功能是模拟自动售货机,您可以在其中放入硬币(美元,四分之一,硬币和镍币),直到您有足够的钱购买糖果。当你有足够的时候,它会给你糖果并告诉你你得到了多少变化。

#include <iostream>
using namespace std;

void countMoney(double change); // þetta fall sér um að telja peninga.
double changeLeft(double money_total); // þetta fall sér um að reikna út afganginn af peningnum

int main()
{

    int a = 0;
    countMoney(a);
    return 0;
}

void countMoney(double change) {

    double dollar=1.00, quarter=0.25, dime=0.10, nickel=0.05;
    double money_total=0.00;
    char answer;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    do {
        cout << "A packet of candie costs $1.50. You have inserted $" << money_total << "." << endl;
        cout << "Please insert coins:" << endl;
        cout << "                n - Nickel" << endl
             << "                d - Dime" << endl
             << "                q - Quarter" << endl
             << "                D - Dollar" << endl;
        cin >> answer;

        if(answer == 'D')
            money_total += dollar;
        else if(answer == 'q')
            money_total += quarter;
        else if(answer == 'd')
            money_total += dime;
        else if(answer == 'n')
            money_total += nickel;
        else
            cout << "\'" << answer << "\' is not a valid coin." << endl;

    } while(money_total <= 1.50);

    cout << "Enjoy your candies. Your change is $" << changeLeft(money_total) << ". Please         visit again." << endl;

}

double changeLeft(double money_total) {

    double change;
    change = money_total - 1.50;

    return change;

}

7 个答案:

答案 0 :(得分:1)

你说,“当我使用美元+季度+季度时,应该是1.50美元,然后循环应该会死,但不会。”这听起来像你想要改变:

while(money_total <= 1.50);

while(money_total < 1.50);

以便在你达到1.50时循环退出。

答案 1 :(得分:1)

将所有数字更改为int,这样您就不会有浮点数比较的问题。

此外,我将<=替换为<,因为如果更改为0,也可以。

#include <iostream>
using namespace std;

void countMoney(); // þetta fall sér um að telja peninga.
int changeLeft(int money_total); // þetta fall sér um að reikna út afganginn af peningnum

int main()
{
    countMoney();
    return 0;
}

void countMoney() {

    int dollar=100, quarter=25, dime=10, nickel=5;
    int money_total=0;
    char answer;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    do {
        cout << "A packet of candie costs $1.50. You have inserted $" << ((float)money_total) / 100 << "." << endl;
        cout << "Please insert coins:" << endl;
        cout << "                n - Nickel" << endl
             << "                d - Dime" << endl
             << "                q - Quarter" << endl
             << "                D - Dollar" << endl;
        cin >> answer;

        if(answer == 'D')
            money_total += dollar;
        else if(answer == 'q')
            money_total += quarter;
        else if(answer == 'd')
            money_total += dime;
        else if(answer == 'n')
            money_total += nickel;
        else
            cout << "\'" << answer << "\' is not a valid coin." << endl;

    } while(money_total < 150);

    cout << "Enjoy your candies. Your change is $" << ((float)changeLeft(money_total))/100 << ". Please visit again." << endl;

}

int changeLeft(int money_total) {

    int change;
    change = money_total - 150;

    return change;

}

答案 2 :(得分:1)

不要使用浮点值处理货币值!您可能会引入导致结果不准确的舍入错误。浮点运算并不精确。

如果您接受货币值作为输入,请通过乘以(10 * decimal places)尽快将它们转换为整数美分。因此,如果您需要精确到2位小数,则乘以100。

如果您将货币显示为输出,请通过除以(10 * decimal places)尽可能晚地将分值转换为浮点数。因此,如果您需要精确到2位小数,则除以100。

仅使用整数运算进行所有数学运算。这样,就没有精度损失,没有舍入错误等等。

例如:

#include <iostream>
using namespace std;

void countMoney();
int changeLeft(int money_total);

int main()
{
    countMoney();
    return 0;
}

void countMoney()
{
    const int dollar = 100, quarter = 25, dime = 10, nickel = 5, penny = 1;
    int money_total = 0;
    char answer;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    do
    {
        cout << "A packet of candie costs $1.50. You have inserted $" << (((double)money_total) / 100)  << "." << endl;
        cout << "Please insert coins:" << endl;
        cout << "                p - Penny" << endl
             << "                n - Nickel" << endl
             << "                d - Dime" << endl
             << "                q - Quarter" << endl
             << "                D - Dollar" << endl;
        cin >> answer;

        if (answer == 'D')
            money_total += dollar;
        else if (answer == 'q')
            money_total += quarter;
        else if (answer == 'd')
            money_total += dime;
        else if (answer == 'n')
            money_total += nickel;
        else if (answer == 'p')
            money_total += penny;
        else
            cout << "\'" << answer << "\' is not a valid coin." << endl;
    }
    while (money_total < 150);

    int change = changeLeft(money_total);
    cout << "Enjoy your candies. Your change is $" << (((double)change) / 100) << "." << endl;
    cout << "Please visit again." << endl;
}

int changeLeft(int money_total)
{
    return money_total - 150;
}

答案 3 :(得分:0)

循环的条件是:

money_total <= 1.50

这意味着一旦你插入1.50,循环就不会 。它一直持续到超过1.50。尝试将其更改为:

money_total < 1.50

答案 4 :(得分:0)

while(money_total <= 1.50)

这就是说“当总数小于或等于 1.5时,保持循环。”换句话说,“当总数大于1.5时,只会突破循环”。您的意思是<,而不是<=

答案 5 :(得分:0)

检查两个双打是否相等(你在while(money_total <= 1.50);中所做的是危险的。 因此,您可以采用不同的方式:

while (money_total < 1.51)假设0.01是你的最小增量(或0.001等)

或者

while ((money_total-1.50) < epsilon)其中epsilon可以是例如boot :: epsilon。

要了解有关此问题的更多信息: How dangerous is it to compare floating point values? http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

答案 6 :(得分:0)

你应该替换

while (money_total <= 1.50)

while (money_total < 1.50 - epsilon)

因为使用quirks of floating point arithmetics,您可以获得1.499999999999之类的总和。

在你的情况下,epsilon的选择非常简单:它应该小于最小硬币,即0.01,所以这样做会很好:

while (money_total < 1.50 - 0.001)