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:
// 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 (constchar * 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 = newint[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.
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?
// Headers
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iomanip>
usingnamespace 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 (constchar * 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 = newint[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 = newint[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.
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.