I have tried everything with this problem and I cannot get it to work. My program complies and I get no errors but it is not catching my exceptions. The exception is that no grade should be less than zero or more than 100. I can find the average and load all the test scores into my array but the program is not catching the exceptions. Thanks for your help in advance.


TestScores header

#ifndef TEST_SCORES_H
#define TEST_SCORES_H
#include <iostream>
using namespace std;

class TestScores
{
private:
	int numScores;  // Number of scores
	int *scores;    // Pointer to array of scores

public:
	
	// Default constructor
	TestScores()
		{ numScores = 0; scores = NULL; }

	// Constructor
	TestScores(int scoreArray[], int size);
	
	// getAverage function
	double getAverage() const;
};

#endif

NegativeScore header

#ifndef NEGATIVE_SCORE_H
#define NEGATIVE_SCORE_H
#include <iostream>
using namespace std;

class NegativeScore
		{
		private:
			double value;
		public:
			

			NegativeScore(double val)
			{value = val;}

			double getValue() const
			{return value;}
		};
#endif

Main File

#include <iostream>
#include <iomanip>
#include <cmath>
#include "TestScores.h"
#include "NegativeScore.h"
using namespace std;


//***********************************************
// Constructor                                  *
//***********************************************

TestScores::TestScores(int scoreArray[], int size)
{
	// Assign numScores.
	numScores = size;

	// Allocate memory for the array.
	scores = new int[size];

	// Copy the array.
	for (int index = 0; index < size; index++)
	{
		if (scoreArray[index] < 0 || scoreArray[index] > 100)
			throw NegativeScore(scoreArray[index]);
		else
		{
			scores[index] = scoreArray[index];
		}
	}
}

//********************************************************
// getAverage function                                   *
//********************************************************

double TestScores::getAverage() const
{
	int total = 0;

	for (int index = 0; index < numScores; index++)
	{
		total += scores[index];
	}
	return static_cast <double> (total / numScores);
}

int main()
{
	int numScores;
	double total = 0;
	TestScores student;
	cout << "How many tests did the student take? ";
	cin >> numScores;

	double *scores;
	scores = new double [numScores];

	cout << "Enter each test score: ";
	for (int count = 0; count < numScores; count++)
	{
		cin >> scores[count];
		total += scores[count];
	}
	

	try 
	{		
		cout << "The average score is: " << total/numScores << endl;
	}
	catch (NegativeScore)
	{
		cout << "Error: This score must be greater than zero but less than 100." << endl;
	}

	cout << "End of the program";
	system ("pause");
	return 0;
}

You need to actually instantiate a TestScores object using the non-default c'tor, which is the only place you actually throw the exception in question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.