This is a C++ question The below code is the result of part 2.
We do not use namespace std at the top of the program
CODE
// This program stores info in variables
#include <iostream>
int sentinel = -1, extraCredit = 3;
void first() {
std::cout <<"This program will calculate the average(%) of
exam grades."<< "\n";
std::cout <<"It will also add extra credit points to "
<<"the exam average given the course difficulty."<<
"\n";
}
int second(){
int grade;
std::cout << "\n";
std::cout <<"Enter an exam grade (type -1 to quit):
\n";
std::cin >> grade;
return grade;
}
float getAverage(int total, int counter){
return (float)((total/counter)+extraCredit);
}
int main() {
int grade,counter = 0,total = 0;
first ();
std::cout <<"Enter all of the grades for one student. "
<<"Type (-1) when finished with that
student."<<"\n";
std::cout <<"If you have additional students, you will be
"
<<"prompted to repeat the program at the
end."<<"\n";
while (std::cin.fail() || grade <= 0 || grade > 1000)
{
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(1000, '\n');
}
do {
grade = second();
if (grade != -1) {
counter++;
total += grade;
}
} while (grade != sentinel);
if (counter > 0) {
std::cout << "Exam average, including extra credit, is: "
<< getAverage(total, counter) << "\n";
}
return 0;
}
}
QUESTION
NEED TO UPDATE MY CODE WITH PART 3
Average Grades (Part 3)
PART 3: Validate and Get Letter Grades
Update the program from Part 2 as follows:
Output:
This program will calculate the average(%) of exam grades.
It will also add extra credit points to the exam average given the
course difficulty.
Enter all of the grades for one student. Type (-1) when finished
with that student.
If you have additional students, you will be prompted to repeat the
program at the end.
Enter an exam grade (type -1 to quit): [user types: ten]
Error: Grades must be an integer 0 or higher.
Enter an exam grade (type -1 to quit): [user types: -10]
Error: Grades must be an integer 0 or higher.
Enter an exam grade (type -1 to quit): [user types: x]
Error: Grades must be an integer 0 or higher.
Enter an exam grade (type -1 to quit): [user types: 100]
Enter an exam grade (type -1 to quit): [user types: 90]
Enter an exam grade (type -1 to quit): [user types: 80]
Enter an exam grade (type -1 to quit): [user types: -1]
Exam average, including extra credit, is: 93
The equivalent letter grade is: A
Notes and Hints:
1) The function that validates grades is now responsible for sending the grade back to main(). It multitasks by getting the input from another function AND validating that input AND it keeps prompting the user until a valid grade is entered.
2) Your textbook has plenty of examples on how to handle scores and letter grades in chp 4. Don't feel obligated to create a constant for each letter grade...save some time here.
TEST 1
INPUT
| ten |
| -10 |
| x |
| 100 |
| 90 |
| 80 |
| -1 |
EXPECTED OUTPUT
| This program will calculate the average(%) of exam grades. |
| It will also add extra credit points to the exam average given the course difficulty. |
| Enter all of the grades for one student. Type (-1) when finished with that student. |
| If you have additional students, you will be prompted to repeat the program at the end. |
| Enter an exam grade (type -1 to quit): |
| Error: Grades must be an integer 0 or higher. |
| Enter an exam grade (type -1 to quit): |
| Error: Grades must be an integer 0 or higher. |
| Enter an exam grade (type -1 to quit): |
| Error: Grades must be an integer 0 or higher. |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Exam average, including extra credit, is: 93 |
| The equivalent letter grade is: A |
TEST 2
INPUT
| 0 | |
| -2 | |
| 100 | |
| -1 |
EXPECTED OUTPUT
| This program will calculate the average(%) of exam grades. |
| It will also add extra credit points to the exam average given the course difficulty. |
| Enter all of the grades for one student. Type (-1) when finished with that student. |
| If you have additional students, you will be prompted to repeat the program at the end. |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Error: Grades must be an integer 0 or higher. |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Exam average, including extra credit, is: 53 |
| The equivalent letter grade is: F |
TEST 3
INPUT
| 70 |
| 43 |
| -1 |
EXPECTED OUTPUT
| This program will calculate the average(%) of exam grades. |
| It will also add extra credit points to the exam average given the course difficulty. |
| Enter all of the grades for one student. Type (-1) when finished with that student. |
| If you have additional students, you will be prompted to repeat the program at the end. |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Exam average, including extra credit, is: 59.5 |
| The equivalent letter grade is: D |
TEST 4
| 80 |
| 53 |
| -100 |
| -1 |
EXPECTED OUTPUT
| This program will calculate the average(%) of exam grades. |
| It will also add extra credit points to the exam average given the course difficulty. |
| Enter all of the grades for one student. Type (-1) when finished with that student. |
| If you have additional students, you will be prompted to repeat the program at the end. |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Enter an exam grade (type -1 to quit): |
| Error: Grades must be an integer 0 or higher. |
| Enter an exam grade (type -1 to quit): |
| Exam average, including extra credit, is: 69.5 |
| The equivalent letter grade is: C |
Code
// This program stores info in variables
#include <iostream>
int sentinel = -1, extraCredit = 3;
void first()
{
std::cout <<"This program will calculate the
average(%) of exam grades."<< "\n";
std::cout <<"It will also add extra credit
points to "
<<"the exam average given the course
difficulty."<< "\n";
}
int validateGrade()
{
int grade;
while(true)
{
std::cout <<"Enter an exam
grade (type -1 to quit): ";
std::cin >> grade;
if(std::cin.fail())
{
std::cout<<"Error: Grades must be an integer 0 or
higher.\n";
std::cin.clear();
std::cin.ignore(1000, '\n');
}
else if(grade<-1 ||
grade>100)
{
std::cout<<"Error: Grades must be an integer 0 or
higher.\n";
}
else
break;
}
return grade;
}
float getAverage(float total, int counter)
{
return ((total/counter)+extraCredit);
}
int main()
{
int grade,counter = 0;
float total = 0;
char letterGrade;
first ();
std::cout <<"Enter all of the grades for one
student. "
<<"Type (-1) when finished with that
student."<<"\n";
std::cout <<"If you have additional students,
you will be "
<<"prompted to repeat the program at the
end."<<"\n\n";
do {
grade = validateGrade();
if (grade != -1)
{
counter++;
total +=
grade;
}
} while (grade != sentinel);
if(counter > 0)
{
double avg=getAverage(total,
counter);
if(avg>=89.5)
letterGrade='A';
else if(avg>=79.5 &&
avg<=89.4)
letterGrade='B';
else if(avg>=69.5 &&
avg<=79.4)
letterGrade='C';
else if(avg>=59.5 &&
avg<=69.4)
letterGrade='D';
else
letterGrade='F';
std::cout << "\nExam
average, including extra credit, is: " << avg <<
"\n";
std::cout<< "The equivalent
letter grade is: "<<letterGrade<<std::endl;
}
return 0;
}
outputs




If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
// This program stores info in variables
#include <iostream>
int sentinel = -1, extraCredit = 3;
void first() {
std::cout <<"This program will calculate the average(%) of
exam grades."<< "\n";
std::cout <<"It will also add extra credit points to "
<<"the exam average given the course difficulty."<<
"\n";
}
int second(){
int grade;
std::cout << "\n";
std::cout <<"Enter an exam grade (type -1 to quit):
\n";
std::cin >> grade;
return grade;
}
int third(){
int grade=0;
while(true){
std::cout <<"\nEnter an exam grade (type -1 to quit):
\n";
std::cin>>grade;
if (std::cin.fail() || grade < -1) {
std::cin.clear();
std::cin.ignore(1000,'\n');
std::cout<<"\n";
}
else
break;
std::cout << "Error: Grades must be an integer 0 or
higher.";
}
return grade;
}
//89.5 - 100 = A; 79.5 - 89.4 = B; 69.5 - 79.4 = C; 59.5 - 69.4
= D; Less than 59.5 = F
char getLetterGrade(float grade){
if (grade >= 89.5 && grade <=100)
return 'A';
else if (grade >= 79.5)
return 'B';
else if (grade >= 69.5)
return 'C';
else if (grade >= 59.5)
return 'D';
return 'F';
}
float getAverage(int total, int counter){
return (float)((total/(float)counter)+extraCredit);
}
int main() {
int grade,counter = 0,total = 0;
first ();
std::cout <<"Enter all of the grades for one student. "
<<"Type (-1) when finished with that
student."<<"\n";
std::cout <<"If you have additional students, you will be
"
<<"prompted to repeat the program at the
end."<<"\n";
do {
//grade = second();
grade = third();
if (grade != -1) {
counter++;
total += grade;
}
else{
grade=sentinel; /// to break from loop
}
} while (grade != sentinel);
if (counter > 0) {
std::cout << "\nExam average, including extra credit, is: "
<< (float)getAverage(total, counter) << "\n";
std::cout<<"The equivalent letter grade is:
"<<getLetterGrade(getAverage(total, counter));
}
return 0;
}
------------------------------------------------------------------------------------------------------------------------



This is a C++ question The below code is the result of part 2. We do...