I am coming to a problem where when negative numbers are entered that it shows an error message. **libc++abi.dylib: terminating with uncaught exception of type std::bad\_alloc: std::bad\_alloc**. is there a way to detect that error when negative numbers and entered with a message: Enter 0 or greater. So, can anyone help me solve this. Thanks for the help.

I tried by doing this: 

```
int students = 0;
    while (students <= 0) {
     cout << "How many students? ";
        cin >> students;
    if (students <= 0) {
      cout << "Enter 0 or greater"; 
    }
}

  int testsScore = 0;
 while (testsScore <= 0) {
     cout << "How many tests per student? "; 
           cin >> testsScore;
   if (testsScore <= 0) {
     cout << "Enter 0 or greater"; 
    }
}

```

Main Code:

```
#include <iostream>  // input out stream library. 
#include <iomanip> // parametic manipulators library for our table. 

using namespace std;

//MARK: Structure to use the following data given in the instructions. 
struct student_information {

    //MARK: Student name.
    string studentName; 

    //MARK: Student ID number.
    int id; 

    //MARK: Pointer to an array of test scores.
    int *tests; 

    //MARK:Average test score.
    double average; 

    //MARK: Course grade.
    char grade; 

   };

//MARK: The main() method should keep a list of test scores for group of students. 
int main() {

    // MARK: Declare variables. 
    int numberOfStudents; 
    int testScores; 

    //MARK: So, in our logic it should ask the user how many test scores there are to be, 
    // and how many students there are. 
    // It should then dynamically allocate an array of structures. However, 
    // each structure tests member should point to a dynamically allocated array
    // that will hold the test scores according to our instructions. 

    cout << "How many students?"; 
    cin >> numberOfStudents; 
    cout << endl; 

     cout << "How many tests per student? "; 
    cin >> testScores; 
    cout << endl;

   student_information *pointerToArray = new student_information[numberOfStudents];

    for (int i = 0; i < numberOfStudents; i++) {

        pointerToArray[i].tests = new int[testScores];

    }

    //MARK: So, this is when the array has been allocated, 
    //the program should ask for the student ID number and all the scores for each 
    // every student. 
    for (int i = 0; i < numberOfStudents; i++) {

        cout << "Student Name: "; 
        cin >> pointerToArray[i].studentName;
        cout << endl; 

        cout << "ID Number: "; 
        cin >> pointerToArray[i].id; 
        cout  << endl;

        for (int v = 0; v < testScores; v++)  {
            cout << " Test # " << (v + 1) << " : "; 

            //MARK: Checking to see all the data is entered for each student / member. 
            cin >> pointerToArray[i].tests[v];
            cout << endl; 

            // MARK: Checking if negative numbers  are entered for any test scores. 
            while(pointerToArray[i].tests[v] < 0 || pointerToArray[i].tests[v] > 100)  {

                cout << "Enter 0 or greater"; 
                cin >> pointerToArray[i].tests[v]; 
               cout << endl; 

            }

       }
    }
    //MARK: The average test score should be calculated and stored in the average member of each structure. 
    //The course grade should be computed on the basis of the following grading scale:
    // Average Test Grade     Course Grade
    // 91-100                          A
    // 81-90                           B
    // 71-80                           C
    // 61-70                           D
    // 60 or below                     F

    for (int i = 0; i < numberOfStudents; i++) {

        double add = 0; 

      for(int x=0; x < testScores; x++)
        add = add + pointerToArray[i].tests[x];
        pointerToArray[i].average = add / testScores;

        //MARK: The course grade will be stored in the grade member structure.
        //After the data entry has been calculated.
    if(pointerToArray[i].average >= 91 && pointerToArray[i].average <= 100)
        pointerToArray[i].grade = 'A';

     else if(pointerToArray[i].average >= 81 && pointerToArray[i].average <= 90)
        pointerToArray[i].grade = 'B';

     else if(pointerToArray[i].average >= 71 && pointerToArray[i].average <= 80)
        pointerToArray[i].grade = 'C';

     else if(pointerToArray[i].average >= 61 && pointerToArray[i].average <= 70)
        pointerToArray[i].grade = 'D';

     else if(pointerToArray[i].average < 60)
        pointerToArray[i].grade = 'F';

        //MARK: Displaying the table to list each students name, ID number, 
         //average test score and the course grade.

        for(int i=0; i < numberOfStudents; i++)
        {

         cout << "Student Name: " << setw(19) << left << pointerToArray[i].studentName <<
         " ID Number: " << setw(19) << pointerToArray[i].id <<
         " Average test score: " <<  setw(15) << pointerToArray[i].average <<
         " Grade: " << setw(15) << pointerToArray[i].grade << endl;

        }

        return 0;

    }

    }

```