Help identify the error pls.

Hello, I was creating a project for my class at Devry. After pouring over the code for 3 days and trying to tweak a few things, I still wasn't able to identify the error. When I run the code it executes until after you press 'q'. Once that happens I get a write error. I asked my instructor to peruse the code as well and he was unable to identify the error either. Please assist.

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
// William_wk5_iLab.cpp : main project file.

#include "stdafx.h"
#include "iostream"
#include "string"
#include "iomanip"

using namespace std;

int InputData(string playerName[], double score[], int &index, int &scoreCount, int ARRAYSIZE);
void DisplayPlayerData(string playerName[], double score[], int &index, int scoreCount, double avgScore);
double CalculateAverageScore(double score[], int &index, int scoreCount);
void DisplayBelowAverage(string playerName[], double score[], int &index, int scoreCount, double avgScore);

int main(array<System::String ^> ^args)
{
         const int ARRAYSIZE = 100;
	int scoreCount, index = 0;
	string playerName[ARRAYSIZE];
	double score[ARRAYSIZE], avgScore;

	cout << fixed << setprecision(2);

	while (1)
	{
		InputData(playerName, score, index, scoreCount, ARRAYSIZE);
		if (playerName[index] == "q" || playerName[index] == "Q")
			break;
	}

	avgScore = CalculateAverageScore(score, index, scoreCount);
	
	DisplayPlayerData(playerName, score, index, scoreCount, avgScore);

	DisplayBelowAverage(playerName, score, index, scoreCount, avgScore);

	system("pause");
         return 0;
}

int InputData(string playerName[], double score[], int &index, int &scoreCount, int ARRAYSIZE)
{
	while (index < ARRAYSIZE)
	{
		cout << "\n\nEnter Player Name (Q to quit): ";
		cin >> playerName[index];
		if (playerName[index] == "q" || playerName[index] == "Q")
			break;

		cout << "\nEnter score for " << playerName[index] << ": ";
		cin >> score[index];
		
		index++;
		scoreCount++;
	}
	return 0;
}

double CalculateAverageScore(double score[], int &index, int scoreCount)
{
	double avg = 0, total = 0;
	
	for (index = 0; index < scoreCount; index++)
	{
		total = total + score[index];
	}

	avg = total / scoreCount;
	return avg;
}

void DisplayPlayerData(string playerName[], double score[], int &index, int scoreCount, double avgScore)
{
	cout << "  Name             Score\n";
	
	for (index = 0; index < scoreCount; index++)
		cout << playerName[index] << setw(19) << score[index] << endl;

	cout << "\n\nAverage Score: " << avgScore << endl;
}

void DisplayBelowAverage(string playerName[], double score[], int &index, int scoreCount, double avgScore)
{
	cout << "\nPlayers who scored below average are:\n\n"
		<< "  Name             Score\n";
	
	for (index = 0; index < scoreCount; index++)
	{
		if (score[index] < avgScore)
			cout << playerName[index] << setw(19) << score[index] << endl;
	}
}
closed account (3qX21hU5)
I am not sure how you don't have any error when you main() is all messed up. It is illegal to declare it like int main(array<System::String ^> ^args) (As far as I know but I am pretty sure it is).


As for the error, the program is stopping in the CalculateAverageScore() function so narrow your search down to there.
This generates the following error:

An unhandled exception of type 'System.AccessViolationException' occurred in wk5_ilab.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
no error, I think all I did was change line 15 and 37

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
// William_wk5_iLab.cpp : main project file.

#include "stdafx.h"
#include "iostream"
#include "string"
#include "iomanip"

using namespace std;

int InputData(string playerName[], double score[], int &index, int &scoreCount, int ARRAYSIZE);
void DisplayPlayerData(string playerName[], double score[], int &index, int scoreCount, double avgScore);
double CalculateAverageScore(double score[], int &index, int scoreCount);
void DisplayBelowAverage(string playerName[], double score[], int &index, int scoreCount, double avgScore);

int main()
{
         const int ARRAYSIZE = 100;
	int scoreCount, index = 0;
	string playerName[ARRAYSIZE];
	double score[ARRAYSIZE], avgScore;

	cout << fixed << setprecision(2);

	while (1)
	{
		InputData(playerName, score, index, scoreCount, ARRAYSIZE);
		if (playerName[index] == "q" || playerName[index] == "Q")
			break;
	}

	avgScore = CalculateAverageScore(score, index, scoreCount);
	
	DisplayPlayerData(playerName, score, index, scoreCount, avgScore);

	DisplayBelowAverage(playerName, score, index, scoreCount, avgScore);

//system("pause");
         return 0;
}

int InputData(string playerName[], double score[], int &index, int &scoreCount, int ARRAYSIZE)
{
	while (index < ARRAYSIZE)
	{
		cout << "\n\nEnter Player Name (Q to quit): ";
		cin >> playerName[index];
		if (playerName[index] == "q" || playerName[index] == "Q")
			break;

		cout << "\nEnter score for " << playerName[index] << ": ";
		cin >> score[index];
		
		index++;
		scoreCount++;
	}
	return 0;
}

double CalculateAverageScore(double score[], int &index, int scoreCount)
{
	double avg = 0, total = 0;
	
	for (index = 0; index < scoreCount; index++)
	{
		total = total + score[index];
	}

	avg = total / scoreCount;
	return avg;
}

void DisplayPlayerData(string playerName[], double score[], int &index, int scoreCount, double avgScore)
{
	cout << "  Name             Score\n";
	
	for (index = 0; index < scoreCount; index++)
		cout << playerName[index] << setw(19) << score[index] << endl;

	cout << "\n\nAverage Score: " << avgScore << endl;
}

void DisplayBelowAverage(string playerName[], double score[], int &index, int scoreCount, double avgScore)
{
	cout << "\nPlayers who scored below average are:\n\n"
		<< "  Name             Score\n";
	
	for (index = 0; index < scoreCount; index++)
	{
		if (score[index] < avgScore)
			cout << playerName[index] << setw(19) << score[index] << endl;
	}
}
Last edited on
The problem in the original code is that after calling function InputData variable index becomes equal to ARRAYSIZE. After that you are trying to access playerName[ARRAYSIZE]

if (playerName[index] == "q" || playerName[index] == "Q")

However acceptable indexes for the array are 0 : ARRAYSIZE - 1
Thank you SamuelAdams. It was indeed the parameters in the main function. Odd that it was that making the error. That is the default value for visual studio 2010 and I have created several programs with it in the past however this is the first one with functions so maybe that is what is making the difference. When I originally learned C++ I always used void main() so I think I will go back to that. Thank you for your help.
ChaosCummings wrote:
When I originally learned C++ I always used void main() so I think I will go back to that.


That would be a retrograde step. main() returns type int
It is specified in the standard for C++.
Topic archived. No new replies allowed.