Skip 3 lines from a file while storing into array

I'm doing an assignment for school and I'm stuck. From the following .txt file:
John
100
99
100
Julio
88.5
80.5
85
Johnathan
70
83.5
86
Julia
80.5
89.9
93

I need to skip the grades to store only the names in the array ""string names[NAMES_SIZE]""


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
  int main()
{
	const int max_line = 65536;
	const int NAMES_SIZE = 4;
	const int GRADES_SIZE = 3;
	string names[NAMES_SIZE];
	double grades[NAMES_SIZE][GRADES_SIZE];
	ifstream inputFile;
	string fileName;
	int count = 0;
	int grade = 0;
	int student = 0;

	cout << "Please enter the file name.\n";
	cin >> fileName;

	inputFile.open(fileName);

	while (!inputFile)
	{
		cout << "File open failure!";
		cout << "Please enter the correct name"
			<< " for the file";
		cin >> fileName;
		inputFile.open(fileName);
		count++;
		if (count == 2)
		{
			cout << "Wrong file name";
			exit(1);
		}
	}

	//Help on this part!!
	for (count = 0; count < NAMES_SIZE; count++)
	{
		inputFile >> names[count];
		cout << names[count] << endl;
		//something here to skip the lines with the grades then move on to reading into the parallel array
	}


Thank you in advance.
I can think of 2 simple ways to do it.

Way 1:
You read the file line by line using a counter starting at 0 and increment it by 1 after the line was read.
If the counter is 0, 4, 8...it is a name and you use it.

Way 2:
You read the file line by line and check if the line is a number. If it is not a number then it must be a name and you can use it.
I'm a beginner and my code knowledge is very poor, would you please show your explanation in code, if you used the same variables names as I did would be really helpful. Thanks
Last edited on
A suggestion: since you have to store all the values in your two arrays:
1
2
	string names[NAMES_SIZE];
	double grades[NAMES_SIZE][GRADES_SIZE];

then there's no need to skip anything. Just read all of the data in the order in which it appears in the file.

In effect, something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
    int count = 0;
    
    while (count < NAMES_SIZE && inputFile >> names[count])
    {
        inputFile >> grade1;
        inputFile >> grade2;
        inputFile >> grade3;
        
        // increment the count only after successfully 
        // reading the name and three grades
        if (inputFile)
            count++;
    }


The while loop condition is worth a closer look, first it checks that count is not bigger than the array size. Then it attempts to read a name. If that succeeds, the body of the loop will then be executed. But you still need to check the status before incrementing the count - if something goes wrong, you need to know how far it got before failure. After the loop completes, the count will contain the number of complete sets of data

Note also, where I put grade1 etc, you need to put the other array in there instead.


Last edited on
closed account (48T7M4Gy)
As already suggested, one of many possible variants:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Help on this part!!
    double dummy = 0;
    int no_to_miss = 3; // Just to generalize it if ever needed
    count = 0;
    while(inputFile >> names[count])
    {
        for(int i = 0; i < no_to_miss; i++)
            inputFile >> dummy;
        
        count++;
    }
    
    // TEST
    for(int i = 0; i < count; i++)
        cout << names[i] << '\n';


Please enter the file name.
skip_items.txt
John
Julio
Johnathan
Julia
Program ended with exit code: 0
Topic archived. No new replies allowed.