Problem I am experiencing: When I compile my code, I get these two error codes:
1) In file included from C:\Users\PaulWilliams\Desktop\CSci Work\C++ Programming\TestScores.cpp
2) unterminated #ifndef

I am not understanding what I did wrong, I have done all of my header files the same way and never had this problem. I was getting more errors, but I took out the default constructor (well, commented it, to see how it would behave. Is the default constructor required for an array, I can't remember if I would have to initialize the elements in the array since I don't have a default constructor, but then its dynamically allocated. Confused on that issue too.)

Task: Write a class named TestScore. The class constructor should accept an array of test scores as its argument. The class hould have a member function that returns the average of the test scores. If any test scores in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in a program. (Here is my code, the header and cpp files.)

THE HEADER FILE:

//******************************************************************************
// Filename:  TestScores.h                                                     *
// Programmer:  Paul Williams                                                  *
// Date:  March 1, 2011                                                        *
// Purpose: This is the Specification file. This header file will declare      *
// the TestScores class, along with its member variables and functions.        *
//******************************************************************************

#ifndef TESTSCORES_H
#define TESTSCORES_H

void testAvg(int len, int marks[]);  // Function prototype

class TestScores       // TestScores class declaration
{
private:
       int _len;                     // To hld the size of the array
       int* _marks[];                // To hold the grades in the array
public:
       /* Default Constructor
       TestScores()
       {
             _len = 0; 
             _marks = 0;
       }*/
       
       // Constructor
       TestScores(int len, int marks[])
       {
    	   _len = len;
    	   *_marks = marks;
       }
           
       // Member Function to get calculate the average
       int getAvg()
       {
    	   int i, j, sum=0, currMark;
    	   char error[60];
    
    	   if(_len < 1) 
           return 0;
    
    	   for (i=0; i<_len; i++)
           {
    		   currMark = (*_marks)[i];
    
    		   // Throwing exception when grade is negative
               if (currMark < 0 )
               {
    			    throw "ERROR:  Grade can not be negative";		  
    		   }
    		   // Throwing exception when grade exceeds 100.               
               if (currMark > 100 )
               {
    			    throw "ERROR:  Grade can not exceed 100 points";
    		   }
    		   sum += currMark;
    	   }
    
          return (sum) / _len;
       }
};

HERE IS MY MAIN FILE:

//******************************************************************************
// Filename:  TestScores.cpp                                                   *
// Programmer:  Paul Williams                                                  *
// Date:  March 1, 2011                                                        *
// Purpose:  This is the Implementation file.  This file will create three     *
// objects of the Test class, one with the correct / restricted data, another  *
// with a test score less than zero, and finally, one with a test score        *
// greater than 100. The average should be calculated for the first one,       *
// While, the exception will be thrown for the last two.                       *
//******************************************************************************

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

int main()
{
	// Test program with and without the exceptions
    // First test will be normal array
	int marks1[] = {79, 89, 86, 100, 82, 94, 73 };
	testAvg(5, marks1);

	// Next, test the array with a negative test grade 
	int marks2[] = {40, -60, 70 };
	testAvg(3, marks2);

	//Finally, test the array with a grade over 100
	int marks3[] = {140, 30};
	testAvg(2, marks3);
	
	system("PAUSE");
	return 0;
}

//******************************************************************************
// The testAvg function will accept an array of grades as an argument          *
// and will return the average of those grades.                                *
//******************************************************************************

void testAvg(int len, int marks[])
{
	TestScores test(len, marks);
	
	int result = 0;
	try
    {
		result = test.getAvg();
		cout << "The average of the test are: " << result << endl;
	}
    catch(char * err)
    {
		cout << err << "\n";
	}
}

Recommended Answers

All 3 Replies

The format of an #ifndef is

#ifndef XXX
// Statements, possibly other pre-processor directives, to happen 
// when XXX is not defined
#endif /* XXX */

So to solve your problem, just add an #endif at the end of your header file.

Problem I am experiencing: When I compile my code, I get these two error codes:
1) In file included from C:\Users\PaulWilliams\Desktop\CSci Work\C++ Programming\TestScores.cpp
2) unterminated #ifndef
I am not understanding what I did wrong,

Unterminated:
Without end; having no termination. from wordnik.com
does not end, conclude, or cease. from dictionary.com

So where is the end of your #ifndef?

L7Sqr and WaltP...thanks for the extra set of eyes that I needed. :) I was going crazy and not realizing that I didn't end it.

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.