In: Computer Science
C++ Coding Exercise (If... Else)
Hello All, I know this question has been asked before, but I was unable to run any of the programs that were posted and I'm stuck. I would like to see an explanation with the code and exactly how to add a .txt file to the program through VS 2019, if possible.
LAB:
Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the header file. You must 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 must print to the console “Excellent” if the grade is greater than or equal to 90, “Well Done” if the grade is less than 90 and greater than or equal to 80, “Good” if the grade is less than 80 and greater than or equal to 70, “Need Improvement” if the grade is less than 70 and greater than or equal to 60, and “Fail” if the grade is less than 60.
//To add text file in VS 2019
pres CTRL+SHIFT +A
and press Add
//C++ code
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//main()
int main()
{
//open the file
ifstream infile("scores.txt");
cout << fixed << setprecision(5);
if (!infile)
{
cout << "File not found..."
<< endl;
return -1;
}
double score, totalPoint;
double percentage = 0;
while (infile >> score >>
totalPoint)
{
percentage = ceil((score /
totalPoint)*100);
cout <<"Grade
"<<percentage<<" %" << endl;
if (percentage >= 90)
{
cout <<
"Excellent" << endl;
}
else if (percentage < 90
&& percentage >= 80)
{
cout <<
"Well Done"<<endl;
}
else if (percentage < 80
&& percentage >= 70)
{
cout <<
"Good" << endl;
}
else if (percentage < 70
&& percentage >= 60)
{
cout <<
"Need Improvement"<<endl;
}
else
{
cout <<
"Fail" << endl;
}
}
//pause
system("pause");
}
//File input
//Output
//If you need any help regarding this solution ........... please leave a comment ....... thanks