Exception handling

Hi guys,

I'm having some issues with exception handling, although I don't know why. I'm probably just tired and over worked but for some reason it's not clicking. I keep getting an unhandled exception error which says is coming from the TestScores::NegativeScore exception. I know this code isn't complete but before I finish this particular problem, I'd really appreciate a pointer on what I'm doing wrong here. 2 files below, the header for the TestScores class and the main cpp. Please take a look.

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
 // TestScores header file
#ifndef TESTSCORES_H
#define TESTSCORES_H

class TestScores
{
public:
	
	int scores[10];
	//*****************************************
	//Negative Score Exception class
	//*****************************************
	class NegativeScore
	{
		private:
			int score;
		
		public:
		
			NegativeScore(double arrayval)
			{
				score = static_cast<int>(arrayval);
			}	
			int getScore()
			{
				return score;
			}
	};
	
	//*****************************************
	//Score larger than 100 Exception class
	//*****************************************
	class TooLargeScore
	{
		private:
			int score;

		public:
			TooLargeScore(double arrayval)
			{
				score = static_cast<int>(arrayval);
			}
			int getScore()
			{
				return score;
			}

	};
	
	//*****************************************
	//Constructor - Takes an array of ints
	//*****************************************
	TestScores(double scores[10])
	{
		scores = scores;

	};
	double getAverage()
	{
		double accum = 0;
		double average = 0;

		for (int count = 0; count < 10; count++)
		{
			if (scores[count] < 0)
			{
				throw NegativeScore(scores[count]);
			}

			if (scores[count] > 100)
			{
				throw TooLargeScore(scores[count]);
			}

			else 
			{

				accum += scores[count];

			}
	
		}
		average = accum / 10;
		return average;
		
	}
};


#endif 


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
//main implementation
#include <iostream>
#include "TestScores.h"

using namespace std;


void menu();

int main ()
{
	const int NUMOFSCORES = 10;
	int userinput;
	double goodScoresArray[NUMOFSCORES] = {92, 84, 98, 72, 77, 68, 88, 94, 96, 59};
	double tooLowScoresArray[NUMOFSCORES] = {-92, 84, 98, 72, 77, 68, 88, 94, 96, 59};
	double tooHighScoresArray[NUMOFSCORES] = {192, 84, 98, 72, 77, 68, 88, 94, 96, 59};
	TestScores myGoodTests(goodScoresArray);
	TestScores myNegativeTest(tooLowScoresArray);
	TestScores myHighTest(tooHighScoresArray);
	menu();
	cin >> userinput;

	switch (userinput)
	{
		
		case 1:
				cout << "This is  the normal array. The average of the scores is: " << myGoodTests.getAverage() << endl;
				menu();
				break;
				
		case 2 :
			try
			{
				cout << "This is the negative array. " << myNegativeTest.getAverage() << endl;
			}
			catch(TestScores::NegativeScore)
			{
				cout << "Negative Score entered!";
			}

		case 3:
			try
			{
				cout << "This is the array with a number larger than 100. " << myHighTest.getAverage() << endl;
			}
			catch(TestScores::TooLargeScore)
			{
				cout <<"Score greater than 100 entered!";
			}
	}
	
	return 0;
}

void menu()
{
	

	cout << " Please indicate which demonstration you would like to perform.\n";
	cout << " Enter the appropriate item number.\n\n\n";
	cout << "1. Demonstrate valid array triggering no exceptions\n";
	cout << "2. Demonstrate a negative test score by throwing the NegativeScore Exception\n";
	cout << "3. Deomstrate a score larger than 100 by throwing TooLargeScore Exception\n";
	cout << "4. Quit  the program\n\n";

	
}
include <exception>

and when you catch somthing, catch with the arg of type const exception&, and then use the member function .what() in cout to display it (if you wish to).
we haven't been introduced to the exception header file as of yet, so I don't think that's the way I'm supposed to do it. Even still, I made the suggested changes and I'm still getting the same unhandled exception error originating in TestScores:NegativeScore.
You have a case of variable shadowing (http://en.wikipedia.org/wiki/Variable_shadowing) in the TestScores constructor, otherwise you would get a compilation error. Try changing the parameter name and recompile:

1
2
3
4
TestScores(double array[10])
{
	scores = array; // error - you have to copy array into scores
} //; 
Wow, I'm not sure how I didn't catch that. I think logically speaking i was thinking I was trying to assign scores the memory address of array and it would work, but I do remember now. Thank you very much!
Topic archived. No new replies allowed.