Memory error

Hey guys, I am trying to code this program. It compiles correctly, but unfortunately somewhere along the way there is a memory issue. When I debug it, it tells me the error is: "Unhandled exception at 0x00081dbc in Assignment6.exe: 0xC0000005: Access violation writing location 0x334d2640." Which occurs in Market.cpp


I've been trying to get this to work for a few days now, its really frustrating!! Please help me!! = (

Here is the code:

//Stocks.h
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;



class Stocks
{
public:
	Stocks(string inputname, string inputsymbol, int inputprice, int fluctuationtype);
	int getPrice();
	string getName();
	string getSymbol();
	int getFluctuationType();
	void setPrice(int inputprice);
	int price;

private:

	void setName(string);
	void setSymbol(string);
	void setFluctuationType(int);
	string symbol;
	string name;
	int fluctuationType;

};



//Stocks.cpp
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
#include "Stocks.h"

Stocks::Stocks(string inputname, string inputsymbol, int inputprice, int inputfluctuationtype)
{

	setFluctuationType(inputfluctuationtype);
	setName(inputname);
	setSymbol(inputsymbol);
	setPrice(inputprice);

}

void Stocks::setPrice(int inputprice)
{
	price = inputprice;
}

void Stocks::setFluctuationType(int inputtype)
{
	fluctuationType = inputtype;
}

void Stocks::setName(string inputname)
{
	name = inputname;
}

void Stocks::setSymbol(string inputsymbol)
{
	symbol = inputsymbol;
}

int Stocks::getPrice()
{
	return price;
}

string Stocks::getName()
{
	return name;
}

string Stocks::getSymbol()
{
	return symbol;
}

int Stocks::getFluctuationType()
{
	return fluctuationType;
}


//Market.h
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Stocks.h"

using namespace std;

const int maxStocks = 500;

class Market
{

public:
	Market(string, int);
	int getDay();
	int getCommission();
	string getMarketName();
	int getStocksArray();

	void listOnMarket(Stocks * stock);
	void displayList();


private:

	int changePrice(int price, int type, int day);

	void setMarketName(string);
	void setCommission(int);

	void setNumStocks(int inputnumstocks);
	int getNumStocks();
	void setList(int i, Stocks* stock);
	Stocks* getList(int i);

	int static day;

	string marketName;
	int commission;
	int numStocks;

	Stocks* list[maxStocks];

};


//Market.cpp
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
#include "Market.h"
#include <iostream>

using namespace std;

int Market::day = 0;

Market::Market(string inputname, int inputcommission)
{
	for (int i = 0; i < getNumStocks() ; i++)
	{
		list[i] -> setPrice(changePrice(list[i] -> getPrice(), i, day));

	}

	day++;
	setCommission(inputcommission);
	setMarketName(inputname);
}

void Market::setMarketName(string inputname)
{
	marketName = inputname;
}

void Market::setCommission(int inputcommission)
{
	commission = inputcommission;
}

int Market::changePrice(int price, int type, int day)
{
	int pricechange = 0;
	switch (type)
	{
	case 0: // swings of 10 dollars or less
		pricechange = -10 + rand() % 10;
		break;
	case 1: //swings of -2 to 5
		pricechange = -2 + rand() % 5;
		break;
	case 2: //swings of -5 to 2
		pricechange = -5 + rand() % 2;
		break;
	case 3: //stagnant
		pricechange = -1 + rand() % 1;
		break;
	default: //something went wrong
		cout << "Error, check code";
	}
	price = price + pricechange;
	return price;
	
}
int Market::getNumStocks()
{
	return numStocks;
}

void Market::setNumStocks(int inputnumstocks)
{
	numStocks = inputnumstocks;
}

void Market::setList(int i, Stocks* stock)
{
	list[i] = stock;
}

void Market::listOnMarket(Stocks* stock)
{
	setList(getNumStocks(), stock);
	setNumStocks(getNumStocks()+1);
}

void Market::displayList()
{
	for (int i = 0; i < getNumStocks(); i ++)
	{
		cout << list[i] -> getPrice() << endl;
		
	}
}


//Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Market.h"

using namespace std;

int main ()
{
	Market Nasdaq("Nasdaq", 5);

	Nasdaq.listOnMarket(new Stocks("Apple","AAPL",180,1));

	

	return 0;
}
Check on line 12 of market.cpp, you have an array of pointers to type Stocks, but they are not initialized to anything (all wild pointers). You need to allocate memory for those pointers using new (inside your loop) before you can start changing the data for them.

I hope that helps, and you can make progress from there.

~psault
Last edited on
Topic archived. No new replies allowed.