c++ - 我的代码中有一个我找不到的错误

标签 c++ debugging

我正在我的 C++ 类中制作一个名为“自动售货机”的程序,除了一个输出,当我使用 Dollar + Quarter + Quarter 时,一切正常,它应该是 1.50 美元,然后循环应该终止,但它惯于。所以我希望这里有人能看到我看不到的东西。该程序的功能是模拟一台自动售货机,您可以在其中投入硬币(美元、25 美分、10 美分和镍币),直到您有足够的钱购买糖果。当你有足够的钱时,它应该给你糖果并告诉你有多少零钱作为返回。

#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;

}

最佳答案

你说,“当我使用 Dollar + Quarter + Quarter 时,它应该是 1.50 美元,然后循环应该结束,但它不会。”这听起来像你想改变:

while(money_total <= 1.50);

while(money_total < 1.50);

这样当您达到 1.50 时循环就会退出。

关于c++ - 我的代码中有一个我找不到的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25858245/

相关文章:

c++ - C++中双括号 “[[foo()]] type name;”语法的含义?

android - "Source Not Found", 在 Eclipse 中调试 Android 应用程序

PHP 5.3 - 解决 500 错误 - 调试 - 解析错误

c++ - 整数作为 8 传递给函数,但函数内部的值是 -439854520

C++ Exe加密?

debugging - jsdom 和 Contextify 错误

c - 使用 malloc 声明变量如何导致丢失位?

c++ - 如何在 x64 Windows 下跟踪调用堆栈?

c++ - GDB/DDD : Debug shared library with multi-process application C/C++

C++ 派生类无法正常运行