< previous page page_224 next page >

Page 224
         {
             cout << Passing;                // Student is passing
             if (average < 70.0)
                 cout <<  but marginal;      // But marginal
             cout << . < endl;
          }
          else                                  // Student is failing
             cout << Failing. < endl;
   }
   else                                      // Invalid data
          cout << Invalid Data: Score(s) less than zero. < endl;

   return 0;
}
Here's a sample run of the program. Again, the input is in color.
Enter a Student ID number and three test scores:
9483681  73  62  68
Student Number: 9483681  Test Scores: 73, 62, 68
Average score is 67.67--Passing but marginal.
And here's a sample run with invalid data:
Enter a Student ID number and three test scores:
9483681 73  -10  62
Student Number: 9483681  Test Scores: 73, -10, 62
Invalid Data: Score(s) less than zero.
In this program, we use a nested If structure that's easy to understand although somewhat inefficient. We assign a value to dataOK in one statement before testing it in the next. We could reduce the code by saying
dataOK = ! (test1 < 0 || test2 < 0 || test3 < 0);
Using DeMorgan's Law, we also could write this statement as
dataOK = (test1 >= 0 && test2 >= 0 && test3 >= 0);
In fact, we could reduce the code even more by eliminating the variable dataOK and using

 
< previous page page_224 next page >