すべてのコードはまだ私の好みに合わせて機能しますが、関数が実行されると初期残高として 0 が入力されるため、顧客名と初期残高は保存されません。私は単純な間違いを犯していますか?主要。cpp
#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"
using namespace std;
int main()
{
char Redo;
string CustomerName;
do
{
float InitialBalance = -1;
float balance1 = 0;
float balance2 = 0;
Account Account;
Account.CreditAccount (balance1, InitialBalance);
Account.DebitAccount (balance2, InitialBalance);
Account.DisplayBalance (CustomerName, balance1, balance2);
//Asks user if they want redo the program
cout << "Would you like to redo the program?\n";
cout << "Please enter Y or N: \n \n";
cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');
char exitchar; //Exit's the program.
cout << "\nPress any key and <enter> to exit the program.\n";
cin >> exitchar;
return 0;
}
Account.h
using namespace std;
class Account {
public:
float balance1;
float balance2;
string CustomerName;
float InitialBalance;
float CreditAccount(float& balance1, float InitialBalance);
float DebitAccount(float& balance2, float InitialBalance);
float DisplayBalance(string CustomerName, float balance1, float balance2);
Account (void);
Account(float balance)
{
SetInitialBalance(balance);
}
void Account::SetInitialBalance(float balance)
{
if(balance >= 0)
{
InitialBalance = balance;
}
else
cout << "Error! Initial Balance cannot be less than 0." << endl;
}
};
Account::Account(void)
{
string CustomerName;
cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl;
cin >> CustomerName;
while(InitialBalance < 0)
{
cout << "Please enter your account balance. No Commas." << endl;
cin >> InitialBalance;
if(InitialBalance < 0)
cout << "Error account balance must be positive." << endl;
}
}
float Account::CreditAccount(float& balance1, float InitialBalance)
{
float CreditInput = -1;
while(CreditInput<0){
cout << "Would you like to credit the account? Enter the amount you would like to credit." << endl;
cin >> CreditInput;
if (CreditInput<0)
cout << "Credit must be positive." << endl;
}
balance1 = (CreditInput + InitialBalance);
return balance1;
}
float Account::DebitAccount(float& balance2, float InitialBalance)
{
float DebitInput = 0;
while((InitialBalance - DebitInput)<0){
cout << "Would you like to debit the account? Enter the amount you would like to debit." << endl;
cin >> DebitInput;
if((InitialBalance-DebitInput)<0)
cout << "You cannot debit more than you have avalaible." << endl;
}
balance2 = (InitialBalance - DebitInput);
if( DebitInput > InitialBalance)
{
cout << "Debit amount exceeds account balance." << endl;
return 0;
}
else
return balance2;
}
float Account::DisplayBalance(string CustomerName, float balance1, float balance2)
{
cout << "Customer Name: " << CustomerName << endl;
cout << "Account Balance for credited account: " << balance1 << endl;
cout << "Account Balance for debited account: " << balance2 << endl;
return 0;
}