2015-02-23 93 views
-1

我正在开发一个项目,在该项目中,我应该使用结构和指针来组织客户的银行帐户信息。我在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; 
} 

回答

1

您的CDAccount * a;CDAccount * initialize_CD_account(double b, double r, int t)只是一个指针,没有为数据分配内存。

要解决一个问题,只需使用类似的东西:

CDAccount *a = new CDAccount; 

编辑:

而且你将有信息更新的烦恼void update_customer_info(CustomerInfo * P)因为你试图与运营商复制char* = (这是将数据存储在自动/堆栈内存中的本地数组的不好的练习)。

只要不使用本地阵列firstName[20]lastName[20],例如:

void update_customer_info(CustomerInfo * P) 
{ 
    cout << "Please enter the customer's new first name:" << endl; 
    cin >> P->first_name; 
    cout << "Please enter the customer's new last name:" << endl; 
    cin >> P->last_name; 
} 

或使用功能复制字符串(例如,函数strncpy())

0

由于错误说,你的变量a未初始化。 你声明了它,但你没有为它分配任何内存。 您可以使用关键字new做到这一点:

CDAccount *a = new CDAccount; 

这为对象分配内存,然后设置a是指针指向的对象。

+0

谢谢!我完全忘了没有新的关键字没有分配内存。我感谢帮助! – mrbarney 2015-02-23 06:36:17