Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This was sample C++ code provided in class however I dont quite understand it, if possible can someone add comments to it describing whats happening?

This was sample C++ code provided in class however I dont quite understand it, if possible can someone add comments to it describing whats happening?

#include #include #include

using namespace std;

double GradePoints(double );

int main() { double percentage_grade = 0.0, grade_point;

cout << fixed << setprecision(1); cout << "Enter percentage grades to convert to grade points. Enter '-1' to quit. ";

while(true) { cout << "Percentage grade: "; cin >> percentage_grade; if (percentage_grade == -1) break; while (cin.fail()){ cout << "Invalid input. Please try again and enter a numeric value." << endl;; cin.clear(); cin.ignore(INT_MAX, ' '); cin >> percentage_grade; } try { grade_point = GradePoints(percentage_grade); cout << endl << percentage_grade << "% is " << grade_point << " grade points." << endl; cout << endl; } catch (const char* msg) { cerr << endl << msg << endl; cout << endl; } }

cout << " Good bye!" << endl << endl; return 1;

}

double GradePoints(double per) { if (per < 0 || per > 100) { throw "An problem occurred: Grade must be between 0.0 and 100.0. Please try again."; }

if (per > 89.5) return 5.0; else if (per > 84.5 && per <= 89.5) return 4.5; else if (per > 79.5 && per <= 84.5) return 4.0; else if (per > 74.5 && per <= 79.5) return 3.5; else if (per > 69.5 && per <= 74.5) return 3.0; else if (per > 64.5 && per <= 69.5) return 2.5; else if (per > 59.5 && per <= 64.5) return 2.0; else if (per > 54.5 && per <= 59.5) return 1.5; else if (per >= 49.5 && per <= 54.5) return 1.0; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions