所有代码都符合我的喜好,我的客户名称和初始余额没有被存储,因为当运行这些功能时,它只输入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;
}
答案 0 :(得分:0)
问题是你已经声明了一个新的局部变量CustomerName
。您正在接收输入但不接受成员变量。
Account::Account(void)
{
string CustomerName; // This is different from the member variable
.....
cin >> CustomerName; // You are taking input to the local variable
// but not to the member variable
答案 1 :(得分:0)
我认为你的问题是,假设&#34; main&#34;中的变量。如果班级成员改变,功能会改变。
int main()
{
char Redo;
string CustomerName;
do
{
float InitialBalance = -1;
float balance1 = 0;
float balance2 = 0;
Account Account;
// [..]
Account.DisplayBalance (CustomerName, balance1, balance2);
// [...]
}while(Redo == 'Y' || Redo == 'y');
}
但是如果您创建一个新的Account
并且从未设置真实的帐户值,则不会存储它们。
如果您在设置为某个值的任何位置创建局部变量,则整个程序没有多大意义。一个例子:
Account::Account(void)
{
string CustomerName;
// [..]
cin >> CustomerName;
// [..]
}
这会在构造函数中创建一个本地变量,该变量不是真正的类成员,并将客户名称存储到此局部变量中。真正的班级成员&#34;客户名称&#34;永远不会设置,并且在构造函数准备就绪后将删除局部变量。
更好:
Account::Account(void)
{
string cname;
// [..]
cin >> cname;
this->CustomerName = cname;
// [..]
}
如果我有任何缺陷,我很抱歉:)