Question

Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

Microsoft Visual Studios 2017

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 <cmath> 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.

Attempted sol'n:

// Header files section
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

// start main function
int main()
{
   // variables declaration
   string fileName;
   string lastName;
   double score;
   double total;
   double grade;
   string description;

   // prompt the user to enter the input file name
   cout << "Enter the input file name: ";
   cin >> fileName;

   // open the input file
   ifstream infile;
   infile.open(fileName);
  
   // exit from the program if the input file does not open
   if (!infile)
   {
       cout << fileName << " file cannot be opened!" << endl;
       exit(1);
   }

   // repeat the loop for all students in the file
   infile >> lastName;
   while (infile)
   {
       infile >> score;
       infile >> total;

       // compute the grade
       grade = score / total * 100;

       // find the grade's description
       if (grade > 90)
           description = "Excellent";
       else if (grade > 80)
           description = "Well Done";
       else if (grade > 70)
           description = "Good";
       else if (grade >= 60)
           description = "Need Improvement";
       else
           description = "Fail";

       // display the result of each student
       cout << lastName << " " << setprecision(0) << fixed << round(grade) << "% " << setprecision(5) << fixed << (grade * 0.01) << " " << description << endl;

       infile >> lastName;
   }

   // close the input file
   infile.close();

   system("pause");
   return 0;
} // end of main function

Program runs, but can't get the input file name.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Header files section
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
// start main function
int main()
{
// variables declaration
string fileName;
string lastName;
double score;
double total;
double grade;
string description;
// prompt the user to enter the input file name
cout << "Enter the input file name: ";
cin >> fileName;
   // open the input file
ifstream infile;
infile.open(fileName.c_str()); //We need to use c_str() method to provide constructor of ifstream const char * (which is what the c_str() method provides.
  
// exit from the program if the input file does not open
if (!infile)
{
cout << fileName << " file cannot be opened!" << endl;
exit(1);
}
// repeat the loop for all students in the file
infile >> lastName;
while (infile)
{
infile >> score;
infile >> total;
// compute the grade
grade = score / total * 100;
// find the grade's description
if (grade > 90)
description = "Excellent";
else if (grade > 80)
description = "Well Done";
else if (grade > 70)
description = "Good";
else if (grade >= 60)
description = "Need Improvement";
else
description = "Fail";
// display the result of each student
cout << lastName << " " << setprecision(0) << fixed << round(grade) << "% " << setprecision(5) << fixed << (grade * 0.01) << " " << description << endl;
infile >> lastName;
}
// close the input file
infile.close();
system("pause");
return 0;
} // end of main function

Add a comment
Know the answer?
Add Answer to:
Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...
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
  • Write a C++ program that computes student grades for an assignment as a percentage given each...

    Write a C++ program that computes student grades for an assignment as a percentage given each student's score and the total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath header file and displayed as a percentage. You must also display the floating-point result up to 5 decimal places. You must use at least 2 functions: one to print the last name of the student and another function to...

  • Write a C++ program that computes a student’s grade for an assignment as a percentage given...

    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 <math> 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. If...

  • For this assignment, you are to write a program that does the following: (C++) • read exam scores...

    For this assignment, you are to write a program that does the following: (C++) • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the monitor: the average score and the total number of scores. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file 2 of 2 //postcondition: inFile is opened. If inFile...

  • For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the mon...

    For this assignment, you are to write a program that does the following: • read exam scores from a file name data.txt into an array, and • after reading all the scores, output the following to the monitor: the average score and the total number of scores. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file 2 of 2 //postcondition: inFile is opened. If inFile cannot...

  • Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score...

    Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score average and Letter grade. No scores less than 0 or greater than 100 should be accepted #include<iostream> #include<string> using namespace std; int main() { double average[5]; string name[5]; char grade[5]; for(int i=0; i<=5; i++) { cout<<"Enter student name: "; cin.ignore(); getline(cin,name[i]); int sum=0; for(int j=0; j<5; j++) { int grades; cout<<"Enter Grades:"<<j+1<<" : "; cin>>grades; sum+=grades; } average[i]=(float)sum/5; if(average[i]>=90 && average[i]<=100) grade[i]='A'; else if(average[i]>=80...

  • For this assignment, you are to write a program that does the following:  read temperatures...

    For this assignment, you are to write a program that does the following:  read temperatures from a file name data.txt into an array, and  after reading all the temperatures, output the following to the monitor: the average temperature, the minimum temperature, and the total number of temperatures read. In addition, the program must use the following functions: //precondition: fileName is name of the file to open, inFile is the input file //postcondition: inFile is opened. If inFile cannot...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Please help the out keeps printing twice #include #include #include #include #include using namespace std; /**...

    Please help the out keeps printing twice #include #include #include #include #include using namespace std; /** * Function to calculate the future value and return the same */ double calculateFutureValue(double presentValue, double interestRate, int months) {    double futureValue = (double)presentValue * pow((1 + interestRate), months);    return futureValue; } /** * Function to read the input file and assign the value to the variables */ unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months) {    inF >>...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

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
ADVERTISEMENT