Loading text file into a dynamic array

Hey guys, new here and I am doing an assignment (yes homework) but I'm kind of stuck..

Pretty much, we have to calculate statistics such as size, sum, median, mode and stddev from a text file of numbers. the file format is as follows:

1
2
5
64
33
.
.
.
etc

I'm doing it step by step so at the moment, I'm loading the file into an array and finding the sum.. here is what I have so far:
header:
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
#pragma once
class Population
{
	int *numbers;
	int rSize;
	bool rSort;
public:
	Population();
	Population(int [], int n);

	Population(const Population &);
	Population & operator = (const Population &); 
	void load(string);
	void add(string);
	int getSize(int arr[], int arrSize);
	int getSum(int arr[], int arrSize);
	float getMean();
	float getMedian();
	float getStddev();
	int *mode(int &);
	~Population(void);
	void printStatistics();

};


implementation

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

#include "Population.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>


using namespace std;


Population::Population(int [], int n)
{
	numbers = new int[n]; // create a dynamic array with numbers to load the text file into

}

void Population::load(string a1test1)
{
	int arrSize;
      ifstream ist(a1test1.c_str()); // open file
      // Check if file opened correctly
      if(ist.fail()) throw runtime_error("file not found");
    
      // Read first value into size attribute
      ist >> arrSize;
	  numbers = new int[arrSize];
	  for (int i = 0; i < arrSize; i++) {
		  ist >> numbers[i];
	  }
 
      // Create and assign new dynamic array to arr
 
      // Read remaining values into array by iterating
      // through the file contents reading each line:
      // ist >> arr[i]
 
      ist.close();
}

int Population::getSum(int arr[], int arrSize) {
	int sum = 0;
	for (int i = 0; i < arrSize; i++) {
		sum = sum + arr[i];
	}
	return sum;
}

Population::Population(const Population & pop)
{
	Popo(pop);
}

void Population::printStatistics() {
	
	
	int size=0;
	int* numbers;

	numbers = new int[size];


	cout << "Sum: " << getSum(numbers, size) << endl;
}
	


Population::~Population(void)
{
	delete [] numbers;
}



main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Population.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
using namespace std;

int main() {
	
	

	Population mypop;
	mypop.getSum();
	mypop.printStatistics();

	system("PAUSE");
	return 0;
}


edited the implementation.. keep getting C2061 errors syntax error identifier 'string'
Last edited on
anyone? sorry if no bumping is allowed..
C2061: Population.h uses undefined identifier "string". A simple forward declaration won't do, so include <string> there.
Do you mean in the header file? if I add it in, I get the same thing.. syntax error: identifier 'string'
Since you've wisely not put using namespace std; in the header file (and that's a good thing - you shouldn't put "using namespace" statements in headers), you'll need to explicitly put the namespace qualifier, i.e. std::string
yeah I read about that when i was researching and reading old threads here.. another problem though. Now I get this:

1>PopulationAssgn1.obj : error LNK2019: unresolved external symbol "public: __thiscall Population::Population(void)" (??0Population@@QAE@XZ) referenced in function _main
1>c:\users\user\documents\visual studio 2012\Projects\PopulationAssgn1\Debug\PopulationAssgn1.exe : fatal error LNK1120: 1 unresolved externals

also the cmd prompt opens up but it says that population.exe is not recognized as an internal or external command.. operable program or batch file

edited the main since getSum is being called in printStatistics.. so didn't wanna call it again:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include "Population.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

using namespace std;

int main() {
	
	

	Population mypop;
	mypop.printStatistics();

	system("PAUSE");
	return 0;
}

Last edited on
Look at what the error message is telling you. It's telling you that you're trying to call Population::Population() (i.e. the default constructor), and it can't find a definition for that.

And if you look at your code, lo and behold - on line 12 of main.cpp, you're attempting to create a Population object with no arguments, which means you're trying to call the default constructor, and in your cpp file, there's no default constructor defined.
Ah that makes sense now. I remember that I don't have a default constructor.. totally forgot about that.

Im confused as to what I would call though in Population.mypop();

Cause now I'm getting 0 for my sum..

thanks for your help btw. finally got one thing somewhat running..

edit:

So far I did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include "Population.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

using namespace std;

int main() {
	
	int *numbers = 0;
	int n=0;
	Population mypop(numbers, n);
	mypop.printStatistics();

	system("PAUSE");
	return 0;
}

Last edited on
anyone? sorry if no bumping is allowed..

edit: redid the function and declared it by itself and not as a member of my class:

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

#include "Population.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

using namespace std;
void printStatistics();

int main() {
	
	printStatistics();
	system("pause");
	return 0;

	
}

void printStatistics() {
	
	int size=0;
	int* numbers=0;

	numbers = new int[size];


	Population mypop(numbers, size);


	cout << "Sum: " << mypop.getSum(numbers, size) << endl;
}


still wont work..
Last edited on
closed account (N36fSL3A)
[deleted]
Last edited on
^ can't use vectors but thanks for your help..
closed account (N36fSL3A)
Yea, read the post and realized my reply was useless, and you probably weren't allowed to use 'em. I tried to delete it in time but then you replied. :P
Last edited on
closed account (D80DSL3A)
I see numerous problems with the code, although the last good look appears in the opening post. Have you changed it much since then?

The biggest problem I see is with your use of dynamic allocation for the array to store the data from your text file.
I see arrays allocated to an int* named numbers all over the place. There's one in the main(), your load(), your printStatistics() and in the constructor.

If the numbers array is to be a member of the Population class then new should appear only in the constructors, operator= and the destructor, and delete should appear only in the destructor (which it does).

How many ways are there for constructing a Population object? If the only intended way is from data in a text file then there should only be one constructor, which itself reads the data from the file (so it could take a std::string as its sole argument). This constructor would be written like your existing load() and would replace it.
As I see your task, I think your class declaration should be more like this:
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
class Population
{
	int *numbers;
	int rSize;
//	bool rSort;
public:
	Population(std::string fileName);
//	Population(int [], int n);

	Population(const Population &);
	Population & operator = (const Population &); 
//	void load(string);
//	void add(string);//         purpose?
//	int getSize(int arr[], int arrSize);
        int getSize();
//	int getSum(int arr[], int arrSize);
        int getSum();
	float getMean();
	float getMedian();
	float getStddev();
//	int *mode(int &);//       purpose?
	~Population(void);
	void printStatistics();

};
I'm a beginner so bear with me please. The reason I have allocated int to numbers in main(), load() because it kept giving me errors that its undefined.

All I'm trying to do at the moment is to load the text file, store it in numbers and use it in getSum. Then make a function called printStatistics and call getSum in there.

Once that's done, I will call printStatistics in main(), however it's not working that well.

As for the header file, there are specific instructions that I have to follow for this assignment so I haven't considered std::string and such.

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

class Population
{
	int *numbers;
	int rSize;
//	bool rSort;
public:
	Population(std::string fileName); <- can't do this.. yet
//	Population(int [], int n);

	Population(const Population &);
	Population & operator = (const Population &); 
//	void load(string);
//	void add(string);//         purpose? <- was in the instructions
//	int getSize(int arr[], int arrSize);
        int getSize();
//	int getSum(int arr[], int arrSize);
        int getSum();
	float getMean();
	float getMedian();
	float getStddev();
//	int *mode(int &);//       purpose? <-- to calculate the mode (haven't written //                                                               that yet)
	~Population(void);
	void printStatistics();

};




UPDATED code:

header

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

#pragma once
#include <string>

using namespace std;
class Population
{
	int *numbers;
	int rSize;
	bool rSort;
	//void Popo(const Population & pop);

public:

	Population(int [], int n);
	Population(const Population & pop); 
	Population & operator = (const Population &); // overloaded operators
	void load();
	void add();
	int getSize(int arr[], int arrSize);
	int getSum(int arr[], int arrSize);
	float getMean();
	float getMedian();
	float getStddev();
	int *mode(int &);
	~Population(void);

};


implementation

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

#include "Population.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>


using namespace std;


Population::Population(int [], int n)
{
	numbers = new int[n]; // create a dynamic array with numbers to load the text file into

}

void Population::load()
{
	  int arrSize;
	  int *numbers;
      ifstream ist;
	  ist.open("a1test1.txt", ios::in); // open file
      // Check if file opened correctly
      if(ist.fail()) throw runtime_error("file not found");
    
      // Read first value into size attribute
      ist >> arrSize;
	  numbers = new int[arrSize];
	  for (int i = 0; i < arrSize; i++) {
		  ist >> numbers[i];
	  }
 
      // Create and assign new dynamic array to arr
 
      // Read remaining values into array by iterating
      // through the file contents reading each line:
      // ist >> arr[i]
 
      ist.close();
}

int Population::getSum(int numbers[], int arrSize) {
	int sum = 0;
	for (int i = 0; i < arrSize; i++) {
		sum = sum + numbers[i];
	}
	return sum;
}

/*
Population::Population(const Population & pop)
{
	Popo(pop);
}
*/




Population::~Population(void)
{
	delete [] numbers;
}


main file

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 "Population.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

using namespace std;
void printStatistics();

int main() {
	
	printStatistics();
	system("pause");
	return 0;

	
}

void printStatistics() {
	
	int size=0;
	int* numbers;

	numbers = new int[size];
	
	Population mypop(numbers, size);
	mypop.load();

	cout << "Sum: " << mypop.getSum(numbers, size) << endl;
}
Last edited on
closed account (D80DSL3A)
I see this in your code #include <string> .
What type of string is that in void load(string); and void add(string); ?
You're not supposed to have a constructor Population(string);?
Last edited on
We were given the member functions and two of them were the load(string) and add(string).

I kept getting errors regarding identifier 'string' so I added the include and and namespace (even though I shouldn't have in the header) and the error went away.

As for Population(string), it wasn't given to us so not using it...
closed account (D80DSL3A)
Of course you must follow your assignment guidelines. I just don't know what those are. Sorry.
Topic archived. No new replies allowed.