Access violation exception thrown - please help

When trying to fill an array of structs, I got the following exception thrown:
Unhandled exception at 0x00007FF61B187C6E in learncpp_chapter6_comp_quiz2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.


This is the full code:
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
#include <iostream>
#include <string>
#include <utility>
#include "keep_window_open.h"

struct Student
{
	std::string name;
	int grade;
};

int takeSizeInput();
void fillArray(Student students[], const int size);
void selectionSortNames(Student students[], const int size);
void displayArray(const Student students[], const int size);

int main()
{
	using namespace std;
	cout << "How many students' names would you like to enter? ";
	int size = takeSizeInput();

	Student *students = new Student[size];
	fillArray(students, size);
	selectionSortNames(students, size);
	displayArray(students, size);

	delete[] students;
 	keep_window_open();
	return 0;
}

int takeSizeInput()
{
	using namespace std;
	int size;
	cin >> size;
	cin.ignore(32767, '\n');
	return size;
}

void fillArray(Student students[], const int size)
{
	using namespace std;
	for (int index = 0; index < size; ++index)
	{
		cout << "Enter name #" << index + 1 << ": ";
		cin >> students[size].name;
		cin.ignore(32767, '\n');
		cout << "Enter grade #" << index + 1 << ": ";
		cin >> students[size].grade;
		cin.ignore(32767, '\n');
	}
}

void selectionSortNames(Student students[], const int size)
{
	using namespace std;
	for (int startIndex = 0; startIndex < size; ++startIndex)
	{
		int largestIndex = startIndex;

		for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			if (students[currentIndex].grade > students[largestIndex].grade)
			{
				largestIndex = currentIndex;
			}
		}

		std::swap(students[startIndex].grade, students[largestIndex].grade);
	}
}

void displayArray(const Student students[], const int size)
{
	using namespace std;
	for (int index = 0; index < size; ++index)
	{
		cout << students[index].name << "\n";
		cout << students[index].grade << "\n";
	}
	cout << "\n";
}


This is keep_window_open.h:
1
2
3
4
5
6
#ifndef KEEP_WINDOW_OPEN_H
#define KEEP_WINDOW_OPEN_H

void keep_window_open();

#endif 


And this is the corresponding .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Function definition for keep_window_open() from the osman_headers.h header file.

#include <iostream>

void keep_window_open()
{
    using namespace std;
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
	cin.ignore();
}


Edit: I was able to fix the exception problem, but now I need to fix the sorting algorithm.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void fillArray(Student students[], const int size)
{
	using namespace std;
	for (int index = 0; index < size; ++index)
	{
		cout << "Enter name #" << index + 1 << ": ";
		cin >> students[size].name; // why are you looping on size?
		cin.ignore(32767, '\n');
		cout << "Enter grade #" << index + 1 << ": ";
		cin >> students[size].grade; // why are you looping on size?
		cin.ignore(32767, '\n');
	}
}


You obviously want to loop on "index", which goes from 0 to (size minus one).

1
2
3
4
5
6
7
8
9
10
11
12
13
void fillArray(Student students[], const int size)
{
	using namespace std;
	for (int index = 0; index < size; ++index)
	{
		cout << "Enter name #" << index + 1 << ": ";
		cin >> students[index ].name; // loop on index
		cin.ignore(32767, '\n');
		cout << "Enter grade #" << index + 1 << ": ";
		cin >> students[index ].grade; // loop on index
		cin.ignore(32767, '\n');
	}
}


Also I really dont understand why all those random ignore lines are there.
Last edited on
Look at this snippet:
1
2
		cout << "Enter name #" << index + 1 << ": ";
		cin >> students[size].name;

And remember that size is the size of the array, therefore you are accessing the array out of bounds. Remember arrays start at zero and stop at size - 1.

You probably want to use index here instead of size.

Have you considered using std::vector instead of the arrays?

Last edited on
Yeah, I got that part fixed. I need to fix the sorting algorithm now, though. The grades to names set is all messed up.
If you use std::vector , you won't need new and delete[] . These are bad news, try to do as much as you can with the STL.
Look at your sort function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void selectionSortNames(Student students[], const int size)
{
	using namespace std;
	for (int startIndex = 0; startIndex < size; ++startIndex)
	{
		int largestIndex = startIndex;

		for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			if (students[currentIndex].grade > students[largestIndex].grade)
			{
				largestIndex = currentIndex;
			}
		}

		std::swap(students[startIndex].grade, students[largestIndex].grade);
	}
}

Why is the swap happening outside the if() statement? Also you should be swapping the whole structure not just the grade.

Something like (using vector):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void selectionSortNames(std::vector<Student>& students)
{
	size_t size = students.size();
	for (size_t startIndex = 0; startIndex < size; ++startIndex)
	{
		int largestIndex = startIndex;

		for (size_t currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
		{
			if (students[currentIndex].grade > students[largestIndex].grade)
			{
				std::swap(students[currentIndex], students[largestIndex]);
			}
		}
	}
}

Last edited on
Topic archived. No new replies allowed.