Help with this C++ code. I\'m using Microsoft Visual Studio 2013. Write a progra
ID: 3807063 • Letter: H
Question
Help with this C++ code. I'm using Microsoft Visual Studio 2013.
Write a program that will calculate the average score and letter grade earned by a student in any course. The program should allow the user to enter the student's name. It should also prompt for the number of exams taken by the student and the score earned for each exam. Calculate the average and letter grade earned. Display the student's name, average, and letter grade. (You must use functions in your source code.) a. Use main() as the driver function Allow the user to run the program as many times as desired. b. Write the function that prompts the user for the name of the student and return sit back to main(). c. Write the function getNumberExams() that prompts the user for the total number of exams taken by the student in the course and returns this value back to main(). Do not allow the user to enter a value less than one for the number of exams. Display an error message and keep prompting until a valid value is entered. d. Write the function getScoresAndCalculateTotal() to determine the total score of the exams take by the student. Use a loop to prompt for each exam score and accumulate the total as the user enters the scores. Return the calculated total back to main(). e. Write the functioncalculateAverage() to determine the average grade earned by the student and return this value back to main(). f. Write the function determineLetterGrade() that determines the letter grade earned by the student (see table above) for the course and returns this back to main(). g. Write the function displayAverageGrade() to display the student's name, the average (to the nearest tenth of a decimal) and letter grade earned by the student. Sample Input/Output: Please enter the student's name: Lisa Jones Please enter the number of exams taken by the student in the course: 3 Enter the exam scores: Exam 1:? 95 Exam 2: ? 85 Exam 3: ? 188 Student Name: Lisa Jones Average: 89.3 Letter Grade Earned: B Would you like to calculate another student's grade? y Please enter the student's name: Nathan Smith Please enter the number of exams taken by the student in the course: 2 Enter the exams scores: Exam 1: ? 91 Exam 2: ? 64 Student Name: Nathan Smith Average: 77.5 Letter Grade Earned: C Would you like to calculate another student's grade? nExplanation / Answer
// C++ code
#include <iostream>
#include <iomanip> // std::setprecision
#include <string.h>
using namespace std;
string getStudentName()
{
string name;
cout << " Please enter the student's name: ";
getline(cin, name);
return name;
}
int getNumberExams()
{
int totalExams;
while(1)
{
cout << "Please enter the number of exams taken by the student in the course: ";
cin >> totalExams;
if(totalExams < 1)
cout << "Invalid Input ";
else
break;
}
return totalExams;
}
double getScoresAndCalculateTotal(int totalExams)
{
cout << " Enter the exam scores: ";
double total = 0;
double score;
for (int i = 0; i < totalExams; ++i)
{
cout << "Exam " << (i+1) << ":? ";
cin >> score;
total = total + score;
}
return total;
}
double calculateAverage(double total, int totalExams)
{
return total/totalExams;
}
char determineLetterGrade(double average)
{
if(average >= 90)
return 'A';
else if(average >= 80)
return 'B';
else if(average >= 70)
return 'C';
else if(average >= 60)
return 'B';
else
return 'F';
}
void displayAverageGrade(string name, double average, char letterGrade)
{
cout << " Student Name: " << name << endl;
cout << "Average: " << setprecision(3) << average << endl;
cout << "Letter grade earned: " << letterGrade << endl;
}
//main function
int main()
{
string name;
int totalExams;
double average, total;
char letterGrade;
char again;
while(1)
{
name = getStudentName();
totalExams = getNumberExams();
total = getScoresAndCalculateTotal(totalExams);
average = calculateAverage(total,totalExams);
letterGrade = determineLetterGrade(average);
displayAverageGrade(name,average,letterGrade);
cout << " Would you like to calculate another student;s grade? <Y or N> ";
cin >> again;
if(again == 'n' || again == 'N')
break;
cin.ignore();
}
return 0;
}
/*
output:
Please enter the student's name: Lisa Jones
Please enter the number of exams taken by the student in the course: 3
Enter the exam scores:
Exam 1:? 95
Exam 2:? 85
Exam 3:? 88
Student Name: Lisa Jones
Average: 89.3
Letter grade earned: B
Would you like to calculate another student;s grade? <Y or N> y
Please enter the student's name: Nathan Smith
Please enter the number of exams taken by the student in the course: 2
Enter the exam scores:
Exam 1:? 91
Exam 2:? 64
Student Name: Nathan Smith
Average: 77.5
Letter grade earned: C
Would you like to calculate another student;s grade? <Y or N> n
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.