What's wrong with this pointer?

So I'm breaking my head here. I've been reading up and down and I just can't figure out why my program crashes once I reach the setLoan function inside of the for loop in main. AM I missing something, or did I implement the pointer wrong? Thanks in advanced.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string>
#include <iostream>
using namespace std;

//Vehicle Class
class Vehicle {
public:
	Vehicle();
	void setPrice(double a);
	void setMpg(int a);
	double getPrice() const;
	int getMpg() const;
	void printVehicle() const;
	Vehicle(double price, int mpg);
private:
	double price;
	int mpg;
};

//Loan Class
class Loan {
public:
	void setBank(string a);
	void setLoan(double a);
	string getBank() const;
	double getLoan() const;
	void printLoan() const;
	Loan(string bank = "", double loan = 0);
private:
	string bank;
	double loan;
};

//Car Class
class Car : public Vehicle {
public:
	Car(double price = 0, int mpg = 0, string bank = "", double loan = 0, string name = "", int element = 0);
	void setName(string a);
	void setLoan(string b, double l, int element);
	string getName() const;
	void printFull() const;
	void setNbrOfLoans(int a);
	~Car();
private:
	string name;
	Loan* pLoan;
	int nbrOfLoans;
};

//Main
int main() {
	Car car1(24800, 22, "Citi", 21600, "Mustang", 1);
	Car car2;
	Car* pCar1 = &car1;
	Car* pCar2 = &car2;
	cout << "Enter the price of the car: ";
	double price;
	cin >> price;
	pCar2->setPrice(price);
	cout << "Enter the mpg: ";
	int mpg;
	cin >> mpg;
	pCar2->setMpg(mpg);
	cout << "Enter the name of the car: ";
	string name;
	cin >> name;
	pCar2->setName(name);
	string bank;
	double loan;
	int index;
	cout << "Enter the amount of loans you obtained: ";
	cin >> index;
	pCar2->setNbrOfLoans(index);
	for (int i = 0; i < index; i++)
	{
		cout << "Enter the name of bank " << i + 1 << ": ";
		cin >> bank;
		cout << "Enter the amount of loan " << i + 1 << ": ";
		cin >> loan;
		pCar2->setLoan(bank, loan, i);
	}
	cout << endl;
	pCar1->printFull();
	pCar2->printFull();
	return 0;
}
Last edited on
Hi,

You don't seem to have defined any of the class functions :+) Do you have the implementation files (*.cpp) anywhere?
Topic archived. No new replies allowed.