read multiple values from file with fstream

Our C++ professor left us an assigment where we need to calculate the avearage grade of 5 students which data is stored in a .txt file.
i.e

Consider the following file format:

Q1,Q2,Q3,Q4,Q5,H1,H2, H3,H4,H4,MTE,FE

Q1,Q2,Q3,Q4,Q5,H1,H2, H3,H4,H4,MTE,FE

Q1,Q2,Q3,Q4,Q5,H1,H2, H3,H4,H4,MTE,FE

Where:

Q# is quiz score,
H# is homework score,
MTE is a midterm exam, and
FE is student’s score for the final exam.

The file with data for 5 students would look something like this:

36,72,58,76,81,73,67,82,38,79,81, 78

63,78,64,86,83,90,92,71,75,89,67, 98

90,88,78,89,91,93,87,92,98,89,85, 98

89,68,79,91,88,75,88,85,56,99,75, 81.
*each line is a different student

I have been looking in forums and web pages and there are good answers, but all of them use arrays or vectors, our professor doesnt want us to use them since this exercise should be the intro for arrays on next class.

my question is, how can I allocate this data in different variables so I can
work with them?***without using arrays or vectors***

Thank you in advance

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream ifs("students_score.txt");
    if( !ifs)
    {
        std::cerr << "File couldn't opened, program abort!";
        return 1;
    }
    
    double total_score= 0;
    int no_of_assignments = 0;
    
    for( double score = 0; ifs >> score; ++no_of_assignments)
    {  
        total_score += score;
    }
    double average_grade = total_score / no_of_assignments;
    
    std::cout << "The average grade of all students is " << average_grade << ".\n";
}

*edit
Sorry, I have overlooked that the values at the file are seperated by commas, shame on me ;)
Last edited on
One of the precursors to using arrays is creating variables with numbered subscripts.
Eg
1
2
int Q1,Q2,Q3,Q4,Q5,H1,H2, H3,H4,H5,MTE,FE;
cin >> Q1>>Q2>>Q3>>Q4>>Q5>>H1>>H2>>H3>>H4>>H5>>MTE>>FE;

Just to demonstrate just how clumsy the approach is.


Then showing how life is so much simpler with arrays and loops.
1
2
int Q[5],H[5],MTE,FE;
for ( i = 0 ; i < 5 ; i++ ) cin >> Q[i];

nuderobmonkey, the grades are separated by commas, not whitespace, so your program will fail when trying to read the second grade.

Salem c, I think the point of the assignment is that you don't have to remember each score. You can just process it and move to the next.

Here is code that will process the input as given. Note that I have not weighted the scores. To weigh them, replace each totalScore += Number with
1
2
totalScore += number * weight;
totalWeight += weight;

and at line 32 replace totalScore / 11.0 with totalScore / totalWeight

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
#include <iostream>

using std::cin;
using std::cout;

int main()
{
    for (int studentNumber=1; true; ++studentNumber) {
	double totalScore = 0.0;
	double number;
	char comma;

	// Read the quizes
	for (int i=0; i<5; ++i) {
	    cin >> number >> comma;
	    totalScore += number;
	}
	// Read the homework assignments
	for (int i=0; i<5; ++i) {
	    cin >> number >> comma;
	    totalScore += number;
	}

	cin >> number >> comma;	// midterm exam
	totalScore += number;

	cin >> number;
	totalScore += number;	// final exam

	if (!cin) break;	// there was a problem reading this line. Hopefully end of file.
	cout << "Average for student " << studentNumber
	     << " is " << totalScore / 11.0 << '\n';
    }
}

Hello nuderobmonkey, salem c and dhyden. Thank you very much for your help. Nuderobmonkey, as dhyden said i had issues reading the data and dhyden, any time I run your code the cmd console is completly blank, I can type any number and press ENTER and the number disapear, also I noticed that in your code there is not IFSTREAM, and i need to open a file were the student grades are .

Again, thank you very much to all for the time and the help

best regards

Rolando
i need to open a file were the student grades are

My program reads from cin, so you can run it with
program < students_score.txt

I tested it with this file:
92,93,88,94,55,72,74,88,94,85,84,92
92,93,88,94,55,72,74,88,93,85,84,92
92,93,88,94,88,72,74,88,94,85,84,92

If you want the program to open the file itself, add std::ifstream ifs("students_score.txt"); at the beginning of the program and then change cin to ifs throughout.
Topic archived. No new replies allowed.