Write a C++ program that computes a student’s grade for an assignment as a perce
ID: 3754093 • Letter: W
Question
Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and the total points. The final score should be rounded up to the nearest whole value 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.
Explanation / Answer
Pease find below the required code, you must have c++ 11 compiler and score.txt file along with cpp file.
//grade.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include<iomanip>
using namespace std;
string get_grade(double score);
int main(int argc, char const *argv[])
{
/*
* TODO: change below the name of
* file if u have diff file name
*/
string file_name = "score.txt";
ifstream fin;
fin.open(file_name,ios::in);
if(!fin){
cout<<"error while opening "<<file_name<<endl;
exit(0);
}
string line;
// read the line in string
getline(fin,line);
// getting the score and total score and parsing
// to double using stod function
double score = stod(line.substr(0,line.find(' ')));
double total_score = stod(line.substr(line.substr(0,line.find(' ')).length(),line.find(' ')));
// rounind off to nearest using ceil function
score = ceil(score);
std::cout << std::fixed;
//for printing upto 5 decimal places
std::cout << std::setprecision(5);
cout<<" -------------------------------------- ";
cout<<"Total score : "<<total_score<<endl;
cout<<"Student score :"<<score<<endl;
cout<<"Student Grade : "<<get_grade(score)<<endl;
cout<<"-------------------------------------- ";
return 0;
}
/*
* methos to return grade basis of score
*/
string get_grade(double score)
{
if (score > 90)
return "Excellent";
else if (score > 80 && score <= 90)
return "Well Done";
else if (score > 70 && score <= 80)
return "Good";
else if (score >= 60 && score <= 70)
return "Well Done";
return "Fail";
}// end of code
//text file
//score.txt
87.3 100
//OUTPUT
--------------------------------------
Total score : 100.00000
Student score :88.00000
Student Grade : Well Done
--------------------------------------
//Please do let me know if u have any concern...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.