In: Computer Science
/* Homework Lab 6
This program does not work. There are three errors that you will need to fix:
TODO: Fix the following 3 errors.
ERROR #1) The compiler knows there is something wrong, and it
will issue a
warning and and two error messages, all related to the same
problem.
Read the error and warning messages
carefully to point you in the
right direction. Did you know if
you double-click on an error
message (in the Error List) that it
will take you to the spot in
your program where the compiler
detects the problem? Also, get
in the habit of ALWAYS reading
messages carefully. You may not
understand them right now, but over
time you will come to understand
the messages better and better. One
of these three messages will be
most helpful, but it is still
informative to see that the other two
messages come as a result of this
single problem.
ERROR #2) After fixing ERROR #1 your program will run. However,
you will get
incorrect results. It will say "Congratulations!" even when not
perfect.
ERROR #3)
But when you find and fix that error, something still lurks
behind that the
compiler does not complain about and that will give incorrect
output. The output
will still display a zero after the decimal point even if it should
be .3 or .7...
IMPORTANT NOTE ON ERROR #3: You are NOT allowed to change the
data type of any constant
or variable to make this work. You also MUST divide by
NUM_OF_SCORES
when calculating the average.
*/
//------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
int score1; // To hold three test scores
int score2;
int score3;
double average; // To hold the average score
const int NUM_OF_SCORES = 3;
// Get the three test scores.
cout << "Enter 3 test scores and I will average
them: ";
cin >> score1 >> score2 >>
score3;
// Calculate and display the average score.
average = (score1 + score2 + score3) /
NUM_OF_SCORES;
cout << fixed << showpoint <<
setprecision(1);
cout << "Your average is " << average
<< endl;
// Follow up with a message.
if (average = 100);
cout << "Congratulations!
Those are all perfect scores" << endl;
else
cout << "Not perfect yet -
Better luck next time." << endl;
cout << endl;
return 0;
}
/* Sample program interactions:
------------------------------------------------------------------------------
BEFORE FIXING ERROR #1:
======================
The program will not run!
AFTER FIXING ERROR #1:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
AFTER FIXING ERROR #2:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
Test #3 (use calculator to see what this average should
be):
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 72 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
AFTER FIXING ERROR #3:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
Test #3:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 72 80 90
Your average is 80.7
Not perfect yet - Better luck next time.
Press any key to continue . . .
*/
#include <iostream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
int score1; // To hold three test scores
int score2;
int score3;
double average; // To hold the average score
const int NUM_OF_SCORES = 3;
// Get the three test scores.
cout << "Enter 3 test scores and I will average them:
";
cin >> score1 >> score2 >> score3;
// Calculate and display the average score.
average = (double)(score1 + score2 + score3) /
NUM_OF_SCORES;//eoror3 type casting needed
cout << fixed << showpoint <<
setprecision(1);
cout << "Your average is " << average <<
endl;
// Follow up with a message.
if (average == 100)//error1 is semicolon at end and error 2 is
relational operator "==" is requried, because '=' operator assigns
100 not checks the condition
cout << "Congratulations! Those are all perfect scores"
<< endl;
else
cout << "Not perfect yet - Better luck next time." <<
endl;
cout << endl;
return 0;
}