Reading from a file and storing it in arrays

Essentially, I have an assignment where I must read from a file and store the data from the file into an array and a 2D array.
The file says this:
John Smith, 5, 10, 15, 10, 11, 14,
Mary Doe, 4, 15, 13, 25, 10, 13,
Larry Tran, 3, 14, 20, 12, 18, 11,
Tom Johnson, 5, 10, 11, 15, 17, 12,
Bella Morrison, 4, 12, 18, 13, 18, 14,
$


It is like a class with the integers being scores. I must store the names into a 1 dimensional array, and store the scores in a 2 dimensional parallel array. I have stored the names into an array, at least I believe I have, after having done some searching in the forum. However I am still having trouble finding out how to store the scores.


My code currently looks 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
26
27
28
29
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
     const int scores = 6;
     ifstream myfile;
     myfile.open("CPSC121data.txt");
     string names[5];
     char delim = ',';
     string numbers;
	
     if (myfile)
       {
	   for (int i = 0; i < 5; i++)
	     {
	while (getline(myfile, names[i], delim) && getline(myfile, numbers))

	     }
       }




	system("PAUSE");
	return 0;
}
A 2d array looks like this:int score[5][scores];
To fill the score array you need an inner loop like:
1
2
3
4
5
6
7
8
9
10
11
	   for (int i = 0; i < 5; i++)
	     {
	while if(getline(myfile, names[i], delim) && getline(myfile, numbers))
{
	   for (int score_idx = 0; score_idx < scores; score_idx++)
	     {
	     score[i][score_idx]=...;
...
	     }
}
	     }
Hello JZam,

Welcome to the forum.

coder777 has one idea that will work with what you have, but will need more than is shown here.

I have added a little to what you started with to give you an idea of what you can do until I have a chance to make what I have shown you work. The comments in the code say most of what I want. Any questions just ask.

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
//#include <iostream>
//#include <fstream>
//#include <string>

//  <--- My basic includes.
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <chrono>
#include <thread>
// <--- Then anything extra.
#include <fstream>

//using namespace std;  // <--- Best not to use.

constexpr unsigned int MAXSiZE{ 5 };
constexpr unsigned int MAXGRADES{ 6 };

int main()
{
	const int scores = 6;
	std::ifstream myfile;
	myfile.open("CPSC121data.txt");
	std::string names[MAXSiZE];
	int grades[MAXSiZE][MAXGRADES];  // Added.
	char delim = ',';
	std::string numbers;

	//if (!myfile)
	//{
	//	// <--- Error message.
	//	std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread".
	//	exit(1);  // <--- 1 determines where it came from, any number other than zero can be used,and that it was not a normal exit.
	//}

	if (myfile)  // <--- Try (!myfile), print an error message and put everything else after the if statement.
	{
		for (int i = 0; i < 5; i++)  // <--- The for loop is unnecessary.
		{
			while (std::getline(myfile, names[i], delim) && std::getline(myfile, numbers))
			{   // <--- Missing the opening brace.
				// <--- Number grades should be read into the array here. Not in the whle condition as a string.
				// This would create more processing to go from string to an int variable, i.e., the array.
				// A nested for loop, two for loops, are needed.
				// After the last grade read you may need to use "myfile.ignore". Same format as the "cin.ignore
				// (...) at line 52. You aare just changing the stream it is used on.
			}
	}

	// <--- Next two line to replace "system".
	// std::cout << "\n\n Press Enter to continue";
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.

	system("PAUSE");  // <--- Best not to use "system" anything.
	
	return 0;
}


Hope that helps,

Andy
So the for loop is before the while isn't needed? I'm still not exactly clear on how to read in the number grades either. As it turns out, the way I stored the names didn't exactly work either because all 5 names were stored into one cell in the array.

edit: as it turns out the names are recorded correctly. Now I just have to give the scores a go.
Last edited on
Topic archived. No new replies allowed.