首页 > 解决方案 > 如何找到我在 C++ 代码中犯的错误?

问题描述

在此处输入图像描述

在输出中,我不断得到 -"dollars" 和 -"cents" 而不是如图所示的正数。

我使用了一个名为储蓄账户的类来设置初始余额、存款和取款。我被要求使用 1 个提示用户输入的对象和另一个使用重载构造函数来初始化美元和美分的对象。

// Include Section
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class SavingsAccount
{
public:
    SavingsAccount();
    SavingsAccount(int, int);

    void setInitial(int, int);
    void setDeposit(int, int);
    void setWithdraw(int, int);
    void output();

private:
    int dollars;
    int cents;
};

// Main Function
int main()
{
    // object declaration

    // Bank 1
    SavingsAccount bank1; //has its values set during definition by the user 

    //Bank 2
    SavingsAccount bank2(200, 50); //uses the constructor to store values into member variables
    bank2.setDeposit(40, 50);
    bank2.setWithdraw(100, 98);
    bank2.output();
    cout << "\n\n";

    // Variable declaration
    string repeat;
    int d, c;
    int choice;

    // Prompt for initial balance
    cout << "welcome to your savings account! Please fill in the appropriate information.\n\n";
    cout << "Initial balance (dollars): ";
    cin >> d;
    cout << "Initial balance (cents): ";
    cin >> c;


    bank1.setInitial(d, c);

    do
    {
        cout << "Pick an option: " << endl
            << "1. Deposit money" << endl
            << "2. Withdraw money" << endl;
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Deposit amount (dollars): ";
            cin >> d;
            cout << "Deposit amount (cents): ";
            cin >> c;
            bank1.setDeposit(d, c);
            break;
        case 2:
            cout << "Withdraw amount (dollars): ";
            cin >> d;
            cout << "Withdraw amount (cents): ";
            cin >> c;
            bank1.setWithdraw(d, c);
            break;
        case 3:
            break;
        default:
            while (choice != 1 && choice != 2)
            {
                cout << "Invalid choice,  enter 1 or 2: \n";
                cin >> choice;
            }
        }

        // Display output
        bank1.output();

        // Prompt for continuing
        cout << "\nWould you like to keep going? (y or Y for yes)";
        cin >> repeat;

    }while (repeat == "y" || repeat == "Y");

    cout << "End Program.\n\n";

    return 0;
}

SavingsAccount::SavingsAccount()
{
    dollars = 0;
    cents = 0;
}

SavingsAccount::SavingsAccount(int newD, int newC)
{
    dollars = newD;
    cents = newC;
    cout << "Your initial balance is $" << dollars << "." << cents << endl;
}

void SavingsAccount::setInitial(int initialD, int initialC)
{   
    while (initialC >= 100)
    {
        initialC = initialC - 100;
        initialD++;
    }
    dollars = initialD;
    cents = initialC;

    cout << "Your initial balance is $" << dollars << "." << cents << endl;
}

void SavingsAccount::setDeposit(int depD, int depC)
{

    while (depC >= 100)
    {
        depC = depC - 100;
        depD++;
    }
    dollars = depD + dollars;
    cents = depC + cents;

    cout << "Depositing $" << depD << "." << depC << " to your account...\n";
}

void SavingsAccount::setWithdraw(int withD, int withC)
{   
    while (withC >= 100)
    {
        withC = withC - 100;
        withD++;
    }

    if (withD > dollars)
    {
        cout << "Not enough money to be withdrawn.\n";
    }
    else
    {
        dollars = withD - dollars;
        cents = withC - cents;
        cout << "Withdrawing $" << withD << "." << withC << " from your account...\n";
    }

}

void SavingsAccount::output()
{
    cout << "dollars = " << dollars << "  cents = " << cents << endl;
}

标签: c++math

解决方案


你问我如何才能找到我在 C++ 代码中犯的错误?(而不是我在 C++ 代码中犯的错误是什么?

您的问题的部分答案是,当您第一次开始学习编程时,这尤其适用于小程序,用铅笔和纸跟踪程序的执行。然后,随着您的程序变得更长和更复杂,开始使用调试器观察它们的执行情况。


推荐阅读