I need help for programming and problem solving with c++ sixth edition and the p
ID: 3762042 • Letter: I
Question
I need help for programming and problem solving with c++ sixth edition and the problem is "Write a C++ program that computes student grades for an assignment as a percentage given the student's score and total points. The final score should be rounded up to the nearest whole values using the ceil function in the <cmath> header file. You should also display the floating -point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by a space. In addition, you should print to the console "Excellent" if the grade is greater than 90, "Well Done" if the grade is greater than 80, "Good" if the grade is greater than 70, "Need Improvement" if the grade is greater than or equal to 60, and "Fail" if the grade is less than 50. The main function is responsible for reading the input file and passing the appropriate arguments to your functions. Here is an example of what the input file might look like:
Weems 50 60
Dale 51 60
Richards 57 60
Smith 36 60
Tomlin 44 60
Bird 45 60"
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
//Given the name, score, and totalPoints, it prints the score, and returns the percentage, rounded to the next integer.
int grader(string name, int score, int totalPoints)
{
int percent;
cout<<"Hello "<<name<<"..."<<endl;
cout<<"Your score is: "<<fixed<<setprecision(5)<<(double)score/totalPoints * 100<<endl;
percent = ceil((double)score/totalPoints * 100);
return percent;
}
//Given the percent, it just comments on the percentage.
void comments(int percent)
{
cout<<"Final Verdict: ";
if(percent >= 90)
cout<<"Excellent..."<<endl;
else if(percent >= 80)
cout<<"Well done..."<<endl;
else if(percent >= 70)
cout<<"Good..."<<endl;
else if(percent >= 60)
cout<<"Need Improvement..."<<endl;
else
cout<<"Fail..."<<endl;
}
int main()
{
string file, name;
int score, totalPoints, percent;
cout<<"Enter the input file name: "; //Read the file name.
cin>>file;
ifstream ip; //Opens the file to read from.
ip.open(file);
while(!ip.eof()) //Reads the name, score, and totalPoints from the file.
{
ip>>name;
ip>>score;
ip>>totalPoints;
percent = grader(name, score, totalPoints); //Passes the read values to the function, to calculate percentage.
comments(percent); //Passes the percent to the function, for appropriate comment.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.