我正在开发一个项目,在该项目中,我应该使用结构和指针来组织客户的银行帐户信息。我在initialize_CD_account
函数中发生错误。C++在函数中使用指针与结构
错误消息说
我不知道是什么问题,“‘A’使用未初始化的局部变量”,任何帮助表示赞赏!
#include <iostream>
using namespace std;
struct CDAccount
{
double balance;
double interest;
int term;
};
struct CheckingAccount
{
double balance;
double interest;
};
struct CustomerInfo
{
char * first_name;
char * last_name;
};
struct Account
{
CustomerInfo * cP;
CheckingAccount * ckP;
CDAccount * cdP;
};
void initialize(Account &);
void initialize_customer_info(CustomerInfo *);
void initialize_Checking_account(CheckingAccount *, double, double);
CDAccount * initialize_CD_account(double, double, int);
void update_customer_info(CustomerInfo *);
double calculate_total_balance(Account);
int main()
{
Account account;
initialize(account);
cout << endl;
cout << "The name of the customer is " << account.cP->first_name << " " << account.cP->last_name << endl;
cout << "The balance in Checking Account is " << account.ckP->balance << endl;
cout << "The balance in CD Account is " << account.cdP->balance << endl;
cout << endl;
update_customer_info(account.cP);
double total_balance = calculate_total_balance(account);
cout << endl;
cout << "The name of the customer is changed to " << account.cP->first_name << " " << account.cP->last_name << endl;
cout << "The total balance is " << total_balance << endl;
system("pause");
return 0;
}
void initialize(Account & a)
{
a.cP = new CustomerInfo;
initialize_customer_info(a.cP);
a.ckP = new CheckingAccount;
initialize_Checking_account(a.ckP, 2000, 0.02);
a.cdP = initialize_CD_account(1000, 0.05, 5);
}
void initialize_customer_info(CustomerInfo * P)
{
char * first = new char[];
char * last = new char[];
cout << "Enter customer's first name:";
cin >> first;
cout << "Enter customer's last name:";
cin >> last;
P->first_name = first;
P->last_name = last;
return;
}
void initialize_Checking_account(CheckingAccount * P, double b, double r)
{
P->balance = b;
P->interest = r;
}
CDAccount * initialize_CD_account(double b, double r, int t)
{
CDAccount * a;
a->balance = b;
a->interest = r;
a->term = t;
return a;
}
void update_customer_info(CustomerInfo * P)
{
char firstName[20];
char lastName[20];
cout << "Please enter the customer's new first name:" << endl;
cin >> firstName;
cout << "Please enter the customer's new last name:" << endl;
cin >> lastName;
P->first_name = firstName;
P->last_name = lastName;
}
double calculate_total_balance(Account a)
{
double total = 0;
total = total + a.ckP->balance;
total = total + a.cdP->balance;
return total;
}
谢谢!我完全忘了没有新的关键字没有分配内存。我感谢帮助! – mrbarney 2015-02-23 06:36:17