0

そこで、残高を追跡する基本的なプログラムを作成しようとしています。引き出しを行い、預金を行い、プログラムを完全に終了することができます。これがコードです。

#include <iostream>
#include <string>
using namespace std;

double balance = 0, withdraw = 0, deposit = 0;
string choice;

class Bank
{
    public:
        void withdrawMoney()
        {
            if(balance - withdraw >= 0)
            {
                balance = balance - withdraw;
            }
            else
            {
                cout << "$5 penalty for attempting to withdraw more than you have.";
                balance -= 5;
            }
        }
    public:
        void depositMoney()
        {
            balance = balance + deposit;
        }
};
int main()
{
    Bank bankObject;
    cout << "Welcome to the Bank Program!" << endl;
    while(true)
    {
        cout << "Would you like to make a withdrawal, a deposit, or quit the program: ";
        cin >> choice;
        if(choice.compare("withdrawal") == 0)
        {
            cout << "Please enter the amount to withdraw: ";
            cin >> withdraw;
            bankObject.withdrawMoney();
            cout << "New balance is: $" << balance << endl;
        }
        else if(choice.compare("deposit") == 0)
        {
            cout << "Please enter the amount to deposit: ";
            cin >> deposit;
            bankObject.depositMoney();
            cout << "New balance is: $" << balance << endl;
        }
        else if(choice.compare("quit") == 0)
        {
            break;
        }
        else
        {
            cout << "Invalid input." << endl;
        }
        cout << "Would you like to try again or quit: ";
        cin >> choice;
        if(choice.compare("quit") == 0)
        {
            break;
        }
    }
    cout << "Thank you for using the Bank Program." << endl;
    return 0;
}

私は C++ は初めてですが (今日から始めました)、Java の経験はあります。メンバー関数の使用が無効であるという最初の if ステートメントで、withdraw メソッドで継続的にエラーが発生します。どんな助けでも大歓迎です。また、問題があるかどうかはわかりませんが、IDE Code::Blocks を使用しています。

編集:最初の問題は修正されましたが、コードを実行すると別の問題が発生しました。コードを実行すると、一度は問題なく通過できますが、try againと入力して2回目を通過しようとすると、ループに陥って応答します最初の質問は「間違った入力」です。ヘルプ?

4

1 に答える 1

3

withdrawグローバル変数とメソッド名の両方として使用します。それらのいずれかの名前を変更すると、この特定のエラーが修正されます。

編集:「再試行」と入力すると、プログラムは「再試行」のみを読み取り(デフォルトでは入力を空白で分割するため)、次の からの読み取りのために「再試行」をバッファに残しますcin。いくつかのデバッグ出力ステートメントで確認できます。

于 2013-08-02T00:49:27.690 に答える