Need help with the code. Struct and Array

Hello, this is my first time using the forum so I wasn't sure where to post so I put it in beginner. This is my homework and I try to get code work, so far I was able to get the code to get information from the text file (name.txt). But for some reason when I pass it around to other function to assign the grade and find the highest grade, it didn't do it job and notified me this:

Exception thrown at 0x0FC965F6 (msvcp140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0xF96C8A1A.

If there is a handler for this exception, the program may be safely continued.

I believe it might be something wrong with my passing with struct but I wasn't sure if it right.
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

#define array_i 20

using namespace std;


ifstream inputname;

struct studentType
{
	string studentFName;
	string studentLName;
	int testScore;
	char grade;
};

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void readStudentData(studentType student[array_i])
{
	inputname.open("name.txt");
		for (int i = 0; i < array_i; i++)
		{
			inputname >> student[i].studentFName >> student[i].studentLName >> student[i].testScore;

		}
	inputname.close();
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void printStudentData(studentType student[array_i])
{
	for (int i = 0; i < array_i; i++)
	{
		cout << student[i].studentFName;
		cout << " " << student[i].studentLName;
		cout << setw(15) << right << student[i].testScore;
		cout << endl;
	}
	cout << endl;
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void assignLetterGrade(studentType student[array_i])
{
	for (int i = 0; i < array_i; i++)
	{
		if (student[i].testScore >= 90)
		{
			student[i].grade = 'A';
		}
		else if (student[i].testScore >= 80)
		{
			student[i].grade = 'B';
		}
		else if (student[i].testScore >= 70)
		{
			student[i].grade = 'C';
		}
		else if (student[i].testScore >= 60)
		{
			student[i].grade = 'D';
		}
		else
		{
			student[i].grade = 'F';
		}
			
	}
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void findHighestScore(studentType student[array_i])
{
	int max = 100;
	for (int i = 0; i < array_i; i++)
	{
		if (student[i].testScore > max)
		{
			student[i].testScore = max;
			cout << "The student with the highest score is: " << endl;
			cout << student[i].studentFName << " " << student[i].studentLName << " with " << student[i].testScore << endl;
		}
	}
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void main()
{	
	studentType student[array_i];

	readStudentData(student);
	printStudentData(student);
	assignLetterGrade(student);
	findHighestScore(student);

	system("pause");
}
are you sure your file has at least 20 entries? A smaller file might crash you.
Yes, I have double check the file I need take the data from. It's exactly 20 entries
I don't see anything else right off. run one function at a time in main, to see which function is crashing.
I try to run each one of the function in main but they all give me the same error.
Could you please show us what the file looks like?
Benjamin James 50
Corine Alise 98
Oscar Amaro 80
Jackson William 87
Allison Sue 23
Kathryn Melissa 56
Philip John 90
Bianca Magali 100
Benjamin Eli 95
Emma Brooks 84
Bojana Jankova 100
Kathleen Jantz 70
Naomi Mast 75
Yanwai Nguyen 73
Kay Ressler 41
Joy Schlabach 79
Ellen Weaver 88
Lynn Wittrig 85
Jieun Han 91
John Graber 68

Also when I try to create a new project on VS and copy paste it to a new cpp file under the new project, the compiler only run the first 2 function but don't run the function to assign the grade and find the highest grade at all
In the function readStudentData(), add the following lines and show us the output afterwards.

1
2
3
4
inputname.open("name.txt");
if(!inputname) perror("File not found "); 
else 
cout << "File successfully opened" << endl;


I am expecting if your program could not open the file from the start.
The file succesfully opened. This is the screenshot of my output.

http://i.imgur.com/tH17FuZ.png
Oh, your program output looks perfectly fine. Where is the exception error?

Exception thrown at 0x0FC965F6 (msvcp140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0xF96C8A1A.

If there is a handler for this exception, the program may be safely continued.
Yea, it was running with that error an hour ago. But when I try to copy paste and throw the code to another .cpp file under a different project. The code run but now it's not running the last 2 function I have on the code.
This function should do what you need.

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
void findHighestScore(studentType student[array_i])
{
	int i;
	int max = 0;
	for (i = 1; i < array_i; i++)
	{
		if (student[i].testScore > student[max].testScore)
		{
			max = i;
		}
	}

	cout << "The student with the highest score is : " << endl;
	cout << student[max].studentFName << " " << student[max].studentLName << " with " << student[max].testScore << endl;
       
	// Print out the rest with the same highest score	

	for (i = max + 1; i < array_i; i++)
	{
		if (student[i].testScore == student[max].testScore)
		{
			cout << student[i].studentFName << " " << student[i].studentLName << " with " << student[i].testScore << endl;
		}
	}
}


The student with the highest score is :
Bianca Magali with 100
Bojana Jankova with 100
Wow, Thank a lot guys, the problem was in the function to find the highest point. I set my maximum point to a 100 so it didn't output my function out. It was a logical error. Thank you all for your help.
Wow, Thank a lot guys, the problem was in the function to find the highest point. I set my maximum point to a 100 so it didn't output my function out. It was a logical error.

It is not just a logical error. The way you implemented your function is completely wrong.
If it's okay, can you explain to me how I implemented my function completely wrong. Please excuse my English, I'm ESL and would love to learn more if I got any mistake
1
2
3
4
5
6
7
8
9
10
11
12
13
void findHighestScore(studentType student[array_i])
{
	int max = 100;
	for (int i = 0; i < array_i; i++)
	{
		if (student[i].testScore > max)
		{
			student[i].testScore = max;
			cout << "The student with the highest score is: " << endl;
			cout << student[i].studentFName << " " << student[i].studentLName << " with " << student[i].testScore << endl;
		}
	}
}


+ First thing : You did not initialize max properly.
+ Second thing : You are supposed to find max, but you change the student score instead when this is what you are not really supposed to.

student[i].testScore = max;

+ Third thing : Assuming you happen to find a student with higher score, you should quietly process all remaining students instead of outputting the student with "max" score outright. It is because there are unprocessed students whose score could be higher. You should only output the students with the highest score at the end of the function, outside your first for loop.
Last edited on
I see. Thank you for your help. It's true that I wasn't pay a careful detail on this function and does it completely wrong. This really help me pay more attention to what I am typing in the future. Thank you kind sir
Topic archived. No new replies allowed.