Lab 6

/2020_S19/wk6/lab6.cpp

The purpose of this lab is to use the GNU Debugger (GDB) and practice debugging programs that contain runtime errors. Also to create programmer defined exception handling classes for "out of bounds" errors and "invalid range" exceptions.

lab6.cpp wget http://www.cs.csubak.edu/~derrick/cs2020/examples/lab6.cpp

Copy the given lab6.cpp and compile the source code using the -g flag to enable debugging information and -Wall to show all warnings.
g++ -g lab6.cpp -Wall

    Part I - InvalidIndex

  1. Run the program ./a.out and test each option linearly. Try to break the program by either entering an invalid test score with option 1 or trying to print a test score at some index that is beyond the bounds of the int* TestScore::tests array.
    Remember the index value you entered that caused a segmentation fault? You will use this value again later in the lab.
  2. Write an empty InvalidIndex exception class definition. Next, for the TestScore::operator[](const int) write the logic to test whether the passed integer parameter is out of the array bounds (hint: [0..count-1]). If so, throw an InvalidIndex object, otherwise, return tests at index. Save the source code, compile, and run the program again repeating everything you did for step 1.
       A) Why did you program SIGABRT?
  3. Load the executable into the GDB debugger gdb a.out.
    Run the program, same routine as above, and encounter the SIGABRT again. Once the program has been terminated, use the backtrace command and select the frame in main().
       B) What line in main caused the SIGABRT?
    Use the backtrace and frame command again and select the frame in TestScores::operator[]
       C) What line in the overloaded operator[] caused the SIGABRT?
  4. Add the necessary try catch block for case 3 in lab6.cpp that will catch an InvalidIndex object if thrown when using TestScore::operator[](const int). Save, recompile, and run.


  5. Part 2 - InvalidScore

  6. A valid test score should be between the range of [0..100]. Currently, the code does not handle the error whenever a user enters an invalid score for option 1. You will write the class definition for a programmer defined Exception Class called InvalidScore. This class definition will contain a private integer member variable err_score. This class will also contain a default constructor, an overloaded constructor that passes a constant integer by value, a destructor, and a void member function called what().
    Write the class definition with prototypes and full function bodies outside the class definition.
  7. Within the definition for TestScores::inputTestScores(), then within the for loop and after the user has entered a "temp" test score, add the logic: if the temp score is invalid, deallocate tests, set tests to NULL, set count to 0, and throw( InvalidScore(temp) ).
    Otherwise, set tests[i] to temp.
  8. Save, compile, and run in gdb. Set a breakpoint for TestScores::inputTestScores()
    break TestScores::inputTestScores()
    Run the program and step through until a SIGABRT occurs.
    Like before, use the backtrace command and "unwind" the stack to find the frame that contained the initial throw. A throw statement will throw the passed argument to where the initial function call was made.
      D) Which function caused the SIGABRT? Why?
      E) Which frame was the InvalidScore object thrown to? (Which function on the stack?)
  9. Exit out of GDB and open lab6.cpp in vi. The issue here is that the istream& operator>>(istream&,TestScores&) is calling a function that has the potential to throw an exception. Write the missing try catch block that will try to call the inputTestScores() function and catch a const InvalidScore &e object within this overloaded operator>> definition. Within the catch statement, write the line that will rethrow the object back to main()
    throw(e); or throw;.
  10. Since the overloaded operator>> for a TestScore object could throw an exception, you need to write the try catch block for main's case 1 "Input New Test Scores" option. When a const InvalidScore &e is caught, call the InvalidScore::what() from the caught object. Save, recompile, run, and test.
  11. Answer Questions A-E as a /*multi lined comment*/ within your lab6.cpp source code

Show me your completed lab or have your completed lab6.cpp source code and answers:
/2020_S19/wk6/lab6.cpp