Just a bit of advice.
- I wouldn't recommend the cout and cin statements inside of your class. This kindof defeats the purpose of a class. For showdata() function this is ok, but for input you should gather that in main() and pass the variables "name" "address" etc. into your addcust() function.
- In your deposit/withdraw function you are passing in a value called "bal". This has to be passed in and directly added or subtracted from acc_bal.
- Usually when using pointers in a class it is advisable to create a destructor to free the space when the class variable goes out of scope. Otherwise crashes probably will occur. Perhaps you should try "string" instead of "char *". This would eliminate the need for a destructor.
Anyways here is some revised code that you can examine if you want.
Code:
#include <iostream.h> // <iostream> -> <iostream.h>
// #include <conio.h> // Include not required.
// #include <stdlib.h> // Include not required.
class Customer
{
private:
char *name;
char *address;
char *city;
char *pcode;
float acc_bal;
public:
Customer:: Customer()
{
name = new char[20]; // name = NULL will cause crash. Need to allocate
address = new char[25]; // address = NULL will cause crash. Need to allocate.
city = new char[15]; // city = NULL will cause crash. Need to allocate
pcode = new char[10]; // pcode = NULL will cause crash. Need to allocate.
acc_bal = 0.00;
}
Customer::~Customer()
{ // IF pointers are used you need a destructor to free memory allocated or else memory leaks.
delete [] name;
delete [] address;
delete [] city;
delete [] pcode;
}
void addcust()
{
cout << "\nEnter your name: "; cin >> name;
cout << "\nEnter your address: "; cin >> address;
cout << "\nEnter your city: "; cin >> city;
cout << "\nEnter your postal code: "; cin >> pcode;
cout << "\nEnter current account balance: "; cin >> acc_bal;
}
void deposit(/*float bal*/) // Your function accepts a value that is never used. float bal
{
float dep;
cout << "\nEnter amount to be deposited: "; cin >> dep;
// bal = bal + dep; // Line not needed.
acc_bal = acc_bal + dep; // acc_bal = bal does nothing.
}
void withdraw(/*float bal*/) // Your function accepts a value that is never used. float bal
{
float wdraw;
cout << "\nEnter amount to be withdrawn: "; cin >> wdraw;
// bal = bal - wdraw; // Line not needed.
acc_bal = acc_bal - wdraw; // acc_bal = bal does nothing. Change + to -
}
void showdata()
{
cout << "Name: " << name;
cout << "\nAddress: " << address;
cout << "\nCity: " << city;
cout << "\nPostal Code: " << pcode;
cout << "\nAccount Balance: $" << acc_bal << endl;
}
};
int main()
{
Customer cust[10];
//int i; // Move declaration into for loop.
for (int i = 0; i < 10; i++)
{
cust[i].addcust(); // Change function call to addcust(); getdata() doesn't exist.
cust[i].showdata();
}
return 0; // int main() needs to return an integer.
}
Below is an example of how you should really be utilizing your class methods.
Code:
int main()
{
for(int i = 0; i < 10; i++)
{
cout << "\nEnter your name: "; cin >> name;
cout << "\nEnter your address: "; cin >> address;
cout << "\nEnter your city: "; cin >> city;
cout << "\nEnter your postal code: "; cin >> pcode;
cout << "\nEnter current account balance: "; cin >> acc_bal;
cust[i].addcust(name, address, city, pcode, acc_bal);
}
cout << "\nEnter amount to be deposited: "; cin >> dep;
cust[0].deposit(dep);
}
void customer::deposit(float bal)
{
acc_bal = acc_bal + bal;
}
void customer::addcust(char *n, char *a, char *c, char *p, float bal)
{
name = n;
address = a;
city = c;
pcode = p;
acc_bal = bal;
}
Just for future reference.