Problem with program output

I am currently working on a program that will read in the contents of a file to a struct and display the following
Student Name:
Average Score:
Letter Grade:

I am having difficulty with the output as I am getting
Student Name: Pinocchio Mustachio
Average Score: -0.25
Letter Grade: F

Student Name: Cinderella Sparkling
Average Score: 6.92892e+007
Letter Grade: A

Student Name: Beowulf Brussard
Average Score: 1.06366e+006
Letter Grade: A

Student Name: Baba Yaga
Average Score: 1.06392e+006
Letter Grade: A

Student Name: Henry theEighth
Average Score: -0.25
Letter Grade: F

Where am I going wrong? Any suggestions are appreciated.
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 #include<iostream>
#include<fstream>
#include<string>

using namespace std;


struct Student{
   string name;
   int scores[4];
   double average;
   char grade;
};


void GetData(struct Student array[])
{
   fstream file;
   
   file.open("Grading.txt");
  
  
   string data;
   int index=0;
   while(getline(file,data))
   {
       
       array[index].name=data;
       
       for(int i=0;i<4;i++)
       {
           getline(file,data);
           array[index].scores[i];
       }
       index++;
   }
}


void CalcAvg(struct Student array[])
{
    double total=0;
   for(int i=0;i<5;i++)
   {
      
       for(int j=0;j<4;j++)
       {
           total+=array[i].scores[j];  
       }  
      
       double avg=total/4;
       array[i].average=avg;
       
       if(avg>=90)
       {
           array[i].grade='A';
       }
       else if(avg>=80)
       {
           array[i].grade='B';
       }
       else if(avg>=70)
       {
           array[i].grade='C';
       }
       else if(avg>=60)
       {
           array[i].grade='D';
       }
       else{
           array[i].grade='F';
       }
   }  
}


void DisplayData(struct Student array[])
{
   for(int i=0;i<5;i++)
   {
       cout<<"Student Name: "<<array[i].name<<endl;
       cout<<"Average Score: "<<array[i].average<<endl;
       cout<<"Letter Grade: "<<array[i].grade<<endl<<endl;
   }
}

int main()
{
   struct Student array[5];
   
   GetData(array);
   CalcAvg(array);
   DisplayData(array);
  
}
The first thing to troubleshoot is if the file is being opened properly.

Put this after line 20:
1
2
3
4
5
if (!file)
{
    cout << "Error opening file\n";
    return;
}


Second,
1
2
           getline(file,data);
           array[index].scores[i];

(1) You do nothing with the 'data' being filled in by getline.
(2) The second line doesn't do anything. No state is changing here.
Last edited on
What's the format of the data file? Post example lines so that we can see what needs to be parsed.

Note that when passing a struct to a function (or an array of struct), in C++ the word struct is not needed. Also when defining a variable (eg L89).

Ok thanks a bunch, this is harder than I thought it would be. The format of the text data file is as follows:
Pinocchio Mustachio
67
79
86
95
Cinderella Sparkling
88
73
92
93
Beowulf Brussard
77
91
93
85
Baba Yaga
89
79
94
81
Henry theEighth
27
62
71
44
Reading the data is actually easy. The numbers can be obtained just by simple stream extraction. Consider:

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

constexpr size_t NoScore {4};
constexpr size_t MaxStud {5};

struct Student {
	std::string name;
	int scores[NoScore] {};
	double average {};
	char grade {};
};

size_t GetData(Student array[]) {
	size_t stud {};

	std::ifstream file("Grading.txt");

	if (file)
		for (Student s; stud < MaxStud && std::getline(file, s.name); array[stud++] = s)
			for (size_t i {}; i < NoScore; ++i)
				file >> s.scores[i] >> std::ws;

	return stud;
}

void CalcAvg(Student array[], size_t stud) {
	for (size_t i {}; i < stud; ++i) {
		double total {};

		for (size_t j {}; j < NoScore; ++j)
			total += array[i].scores[j];

		const double avg {total / NoScore};

		array[i].average = avg;

		if (avg >= 90)
			array[i].grade = 'A';
		else if (avg >= 80)
			array[i].grade = 'B';
		else if (avg >= 70)
			array[i].grade = 'C';
		else if (avg >= 60)
			array[i].grade = 'D';
		else
			array[i].grade = 'F';
	}
}

void DisplayData(const Student array[], size_t stud) {
	for (size_t i {}; i < stud; ++i) {
		std::cout << "Student Name: " << array[i].name << '\n';
		std::cout << "Average Score: " << array[i].average << '\n';
		std::cout << "Letter Grade: " << array[i].grade << "\n\n";
	}
}

int main() {
	Student array[MaxStud];
	const auto studs {GetData(array)};

	if (studs > 0) {
		CalcAvg(array, studs);
		DisplayData(array, studs);
	} else
		std::cout << "Cannot read data file\n";
}


which displays:


Student Name: Pinocchio Mustachio
Average Score: 81.75
Letter Grade: B

Student Name: Cinderella Sparkling
Average Score: 86.5
Letter Grade: B

Student Name: Beowulf Brussard
Average Score: 86.5
Letter Grade: B

Student Name: Baba Yaga
Average Score: 85.75
Letter Grade: B

Student Name: Henry theEighth
Average Score: 51
Letter Grade: F

Thanks for the pointers, I've managed to figure it out and have it working as intended now.
Topic archived. No new replies allowed.