Xcode error on throw

Without the negative and over 100 it working fine


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
#ifndef TESTSCORES_H
#define TESTSCORES_H

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

class TestScores       // TestScores class declaration
{
private:
    int _len;                     // To hold 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, sum=0, currMark;
        
        if(_len < 1)
            return 0;
        
        for (i=0; i<_len; i++)
        {
            currMark = (*_marks)[i];
            
            // error right here.
            // 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;
    }
};
#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
#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);
	
	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";
	}
}
Last edited on
catch(char * err)
{
cout << err << "\n";
}
}

how can i fix this
Topic archived. No new replies allowed.