Hi I have an assignment where I was given a data file and put it in an array. The file looks somthing like this:

Smith	John	78	70	76	72	82	84
Doe	Jane	72	76	77	90	76	45
Weathers	April	64	93	95	83	66	56
Brown	James	67	78	68	75	64	46
Howard	Dwight	67	75	97	90	57	89
Parker	Peter	67	45	56	79	83	59
Grim	Ben	62	68	76	66	46	76

I know how to open the file and I know how to create aN array, but I don't understand how assign the values to the array. Something tells me this is easy, but I'm stumped =(

couple of ways you could do this, I will show you using a vector object, which will allow you to read in a file of any size. A vector has many of the same behaviors as an array so it's easy to use. Here is a list of all the member functions you get with vector. The push_back() function is most common:


vector<string> file;
string line;

//read in a line of data
while(getline(infile, line))
     //push the data into the back of the vector array
     //unlike an array of fixed size, you can read in a file of any size

//Now you can access or display your data just like an array
for(int i=0, size=file.size(); i<size; i++)
     cout << file[i] << endl;

In summary, vector class objects are easy to use because they exhibit the same behavior as arrays and also provide member functions that allow you to store data of unknown size.

Thank you very much for your input Portis. Fortunately my professor helped me out with this problem. However, I have one more question(I would make a new thread, but I don't think there is a need for another useless thread). I'm trying to find the sum of an array and so far I have this:

int sum(double examGrades[], int arraySize)
	int sum = 0;
	for(int kk = 0; kk < arraySize; ++kk)
		sum = sum + examGrades[kk]
return sum;

I think I have something wrong here, can you help me understand what is missing or what I am doing wrong?

looks good to me. the only things that could go wrong is an incorrect arraySize being passed in, or the examGrades[ ] array wasn't initialized properly (in this case, all elements to zero), which would cause random stuff from memory to reside in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.