using pointers to populate a dynamic array

I am learning C++ and I have an assignment that asks me to write a program that reads in data from a text file and stores the data in a dynamic array. I must pass pointers to my functions and functions should perform pointer arithmetic. I am NOT allowed to use index operators anywhere in my program except when creating my dynamic array. I cannot use vectors. The text file contains several numbers. The first number is meant to be the total of the numbers, so I am using that for the count/size of the array. The other numbers are the elements, which represent movies watched. Here is my code so far:
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
// Global variables
const string FILENAME = "moviecount.txt";

// Function declarations
int readMovieData(int *& movies, string fileName);

int main()
{
	int numStudents;	// number of students who watch movies
	int * movieCounts = nullptr;     // an array of integers to store counts of movies for each student
	

	// Read in the movie data
	try
	{
		numStudents = readMovieData(movieCounts, FILENAME);
	}
	catch (const char * message) // if file cannot be opened then catch the thrown exception
	{
		cout << message << endl;
		system("PAUSE");
		exit(EXIT_FAILURE);
	}
    return 0;
}
/***********************************************************
Reads data in from fileName and stores in movies. movies will point to a dynamic array of integers
PARAM:	movies is a reference to a pointer to int
		fileName is a string that contains the name of the file to be read
PRE:	The file fileName exist in the project folder. The first value in the file is the number of values
		after the first value
POST:	movies points to a dynamic array of integers with the size equal to the first value in the file
		Each element in movies array corresponds to a value in the file
************************************************************/
int readMovieData(int *& movies, string fileName)
{
	ifstream in(FILENAME);
	if (!in)
	{
		throw "file did not open";
	}
	int count = 0;

	in >> count;

	if (count = 0)
	{
		throw "file has no data";
	}

	int * arr = new int[count];
	
	for (int i = 0; i < count; ++i)
	{
		in >> *(arr + i);
	}

	return count;
}


The problem I am having is that when i try to do stuff with the information that is supposed to be coming from the file, it doesnt seem to be reading it correctly. i just get a 0 for the count.
Last edited on
> i just get a 0 for the count.
you set it to 0 here if (count = 0)

also, you don't seem to use the `movies' parameter
I use it in other places, but I didn't copy that here, didn't seem relevant. I see I should have used ==, I knew it was a simple mistake, I just couldn't see it, thanks.
I ran into a new problem, and I'm not sure what it is, but I think it has to do with my pointers.
I try to use the data with this function:

1
2
3
4
5
6
7
8
9
10
11
double calculateAverage(int * movies, int size)
{
	int sum = 0;
	double average;
	for (int i = 1; i < size; ++i)
	{
		sum = *(movies + i) + sum;
	}
	average = sum / size;
	return average;
}


When I get to the line with sum, i get a read access violation. I noticed that while it is reading in in the read function, it has a certain address, but when it moves to the average function, it is back to 0x00000000. Am I passing the array incorrectly? or is it a scope issue?
Your calculateAverage has two problems. In the for loop you skip the first value and you use int for sum.
Am I passing the array incorrectly? or is it a scope issue?

How can we know? You didn't show us how you called the function.
It always best to show the whole code.
i was worried i would have posted too much irrelevant stuff, but here is all the code im using right now.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Headers
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

// Global variables
const string FILENAME = "moviecount.txt";

// Function declarations
int readMovieData(int *& movies, string fileName);
double calculateAverage(int * movies, int size);

int main()
{
	int numStudents;				// number of students who watch movied
	int * movieCounts = nullptr;	// an array of integers to store counts of movies for each student
	

	// Read in the movie data
	try
	{
		numStudents = readMovieData(movieCounts, FILENAME);
	}
	catch (const char * message) // if file cannot be opened then catch the thrown exception
	{
		cout << message << endl;
		system("PAUSE");
		exit(EXIT_FAILURE);
	}
	
	cout << setprecision(2) << fixed << showpoint;	// set decimal places to two for average

	cout << "Total number of students who watched movies is " << numStudents << endl;
	cout << "The average number of movies watched by all students is " << calculateAverage(movieCounts, numStudents) << endl;



	cout << endl;

	
    cout << endl;

	
    system("PAUSE");

    return 0;
}

/***********************************************************
Reads data in from fileName and stores in movies. movies will point to a dynamic array of integers
PARAM:	movies is a reference to a pointer to int
		fileName is a string that contains the name of the file to be read
PRE:	The file fileName exist in the project folder. The first value in the file is the number of values
		after the first value
POST:	movies points to a dynamic array of integers with the size equal to the first value in the file
		Each element in movies array corresponds to a value in the file
NOTE:
************************************************************/
int readMovieData(int *& movies, string fileName)
{
	ifstream in(FILENAME);
	if (!in)
	{
		throw "file did not open";
	}
	int count = 0;

	in >> count;

	if (count == 0)
	{
		throw "file has no data";
	}

	int * arr = new int[count];
	
	for (int i = 0; i < count; ++i)
	{
		in >> *(arr + i);
	}

	return count;

}

/***********************************************************
Calculates the average of the elements in dynamic array movies
PARAM:	movies points to a dynamic array
		size is the number of elements in array pointed to by movies
PRE:	movies points to an array of size elements
POST:	average is calculated and the value returned
NOTE:
************************************************************/
double calculateAverage(int * movies, int size)
{
	double sum = 0;
	double average;
	for (int i = 0; i < size; ++i)
	{
		sum += *(movies + i);
	}
	average = sum / size;
	return average;
}
OK, now things become ckear.
In readMovieData why do you store the data in arr instead of movies ?
In main movieCounts will remain nullptr and you pass this nullptr to calculateAverage
in readMovieData, im not sure how to do it differently, because if i were to do something like int * movies = new int[count]; i get an error because it's a redefinition. I did think there was a problem here because it seemed like the arr goes out of scope, but as for a solution i couldn't figure it out.
Just use the parameter like this: movies = new int[count]; in readMovieData
it's always the simplest little things that get past me. Thank you so much. It is working like I need it to. I might come back if I have issues with the other functions i will be writing, but this was a big help.
Topic archived. No new replies allowed.