Question

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

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:

  • Create a new function to validate an entered grade (do not accept numbers below -1 or nonsense such as letters or words). NOTE: We're going to do something a bit different from what we did in class.
    1. This function should loop until the user provides a valid grade. This means calling the function that handles the input, running the usual loop that handles validation, and then calling the input function again. Once the user gives you something valid, the loop will obviously end, at which point you should return the valid grade to main().
    2. Yes, this means you will end up with a layered function call, where main() calls the validation function, which in turn calls the input function repeatedly until it gets valid input from the user.
    3. And yes, this means that the validate function will not return true or false anymore. It is returning the actual grade to main().
    4. In main(), you must replace the original function calls to the get input function with the function that validates input.
  • Create another new function that reads the average and determines the student's letter 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
    • You will return the letter grade to the main() to be output as shown in the last output line below

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Answer #2

// 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;


}

------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
This is a C++ question The below code is the result of part 2. We do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT