This is the prompt of the assignment, and I cannot get the counter and accumulator to work in the PART 2.
PART 1
- Write a program that displays the introduction/description to the user and prompts for the entry of a single grade.
- The introduction should be in one function and the input should be in another function
- In main(), call the two functions, store the grade from the input function in a variable, and then output that grade.
PART 2
- In main(), add the appropriate loop that keeps asking the user to enter grades as long as the user doesn't enter a Sentinel value (-1)
- In main(), add a counter and accumulator to keep track of how many grades were read and the total sum of the grades
- Once the user enters the Sentinel value, the loop should end. It is now time to average the grades. Call a new function that accepts the running total and counter. Divide the running total by the counter, then add 3 points extra credit to that average. Return this final average to main().
Main() should output that average only if there were any grades read. In other words, if the user ran the program and immediately typed (-1), then don't bother outputting the average as it will return the NaN (not a number) message.
Here is my code so far
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 56 57 58 59 60
|
#include <iostream>
void introduction();
int input();
double computeAvg(int, double);
int main() {
int grade = 0;
const int SENTINEL = -1;
int counter = 0;
double sum = 0.0 ;
double finalAvg;
introduction();
while (grade != SENTINEL)
{
input();
grade = input();
counter++;
sum += grade;
}
finalAvg = computeAvg(counter, sum);
std::cout << "Exam average, including extra credit, is: " << finalAvg ;
return 0;
}
void introduction()
{
std::cout << "This program will calculate the average(%) of exam grades.\n"
"It will also add extra credit points to the exam average given the course difficulty.\n"
"Enter all of the grades for one student. Type (-1) when finished with that student.\n"
"If you have additional students, you will be prompted to repeat the program at the end.\n"
<< std::endl;
}
int input()
{
int inputGrade;
std::cout << "Enter an exam grade (type -1 to quit): " << std::endl;
std::cin >> inputGrade;
return inputGrade;
}
double computeAvg(int counter, double sum)
{
double avg;
const int extraCredit =3;
avg = sum / counter + extraCredit;
return avg;
}
|