Btw below is the key parts of my code - not all as there are loads of lines:

//main.cpp
#include <iostream>
#include "Customer.h"

using namespace std;

void displayMainMenu();
int enterMainChoice();
void displayAccountMenu();
int enterAccountChoice();
int enterCustomerID();
Customer createCustomer();

void main(void)
{
	unsigned short numberOfCustomers = 0;
	Customer *Customers[10];

	*Customers[numberOfCustomers] = createCustomer();
	numberOfCustomers++;
}

Customer createCustomer()
{
	string name;
	int age;
	cout << "\nPlease enter you name: ";
	cin >> name;
	cout << "Please enter you age: ";
	cin >> age;
	return Customer(name, age);
}

----------------
Below is just the main function from Customer.cpp
----------------

Customer::Customer(string name, int age)
{
	this->name = name;
	this->age = age;
}

Now it almost works. I can run it and it asks me for the information, but when it tries to add the actual customer is crashes in "xstring" with this error:

Unhandled exception at 0x643924f0 (msvcp90d.dll) in bankusingarrays.exe: 0xC0000005: Access violation reading location 0xcccccce8.

Now - I thought it would be because I am passing back something which can't be then put into the pointer array.

But I am not sure how to go about it - trying to create the object and store the address in the pointer array. I am pretty new with C++ so try and bare with me lol..

Thanks!

Customer *Customers[10];

You create an array of pointers to that object.

Why don't you do:

Customer Customers[10];
Customer[numberOfCustomers] = createCustomer();

-------------

1.Basically you are misuing pointers. In C and C++, when a variable is uninitialized, it contains a garbage value. Thus, Customers from 0 to 9 point to garbage values. They are pointers, which means they point in unknown places in the memory.

Then, you are assigning a value to that unknown memory location. This is the error. Before assigning, you have to be sure that the pointer points to a valid memory location of that object.
This is what you are doing.

int main()
{
	int *p, x;

	x = 10;
	*p = x;
}

This is what you should be doing.

int main()
{
	int *p, x;

	x = 10;
	p = &x;
}

If you still want an array of pointers to Customer objects, you should write:

Customers[numberOfCustomers] = &(createCustomer()); //the address of the object

2.
Don't use void main(void). Just int main(). The use of void in the parentheses is redundant. It is required in C but not in C++.

Hi
could u please post the Customer.h too?

Hi
could u please post the Customer.h too?

#include <string>
#pragma once

using namespace std;
//----------------------
class Customer
{

private:
	string name;
	int age;

public:
	
	//builder
	Customer(string name, int age);

	//operations
	void Display();
};

the display function doesn't have anything it btw, thats for later

...

Ok thanks dude, briefly looking at it makes total sense, will look over my code to do as you say!

Hi again,
have a look on this it might help u:

#include <string>
#include <iostream>

using namespace std;

const int MAX_NO_OF_CUSTOMERS = 3;

class Customer
{
private:
	string name;
	int age;
	static unsigned short numberOfCustomers;
public:
	Customer()
	{
		createCustomer();	
		numberOfCustomers++;
	}

	Customer(string name, int age)
	{
		this->name = name;
		this->age = age;	
	}

	Customer createCustomer()
	{
		string name;
		int age;
		cout << "\nPlease enter you name: ";
		cin >> name;
		cout << "Please enter you age: ";
		cin >> age;
		return Customer(name, age);	
	}
	int getNumberofCustomers()
	{
		return numberOfCustomers;
	}
};
unsigned short Customer::numberOfCustomers = 0;


int main()
{
	int customersInTotal;
	Customer *Customers = new Customer[MAX_NO_OF_CUSTOMERS];
	customersInTotal=Customers->getNumberofCustomers();
	cout<<"Until now "<<customersInTotal<<" customers have been added."<<endl;
	return 0;
}

i did on a hurry....maybe there are big mistakes :D
However it compiles and runs... :D :D

P.S: keep this in mind:
A no-argument constructor will be called automatically when dealing with an array of objects. But when u define some constructor with arguments, then u need to define a no-argument constructor too.

Have you tried what I told you?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.