/* GPA program (translated from C to C++) gpa.in contains sample input gpa.out contains sample output In order to use them with this program, you must use file I/O redirection in Unix */ #include #include #include using std::cout; using std::cin; using std::endl; using std::string; // function declarations = prototypes double getpoints(string &); // no param names needed int main(void) { string grade; double credits; double points; double totalpoints = 0, totalcredits = 0; cout << "enter grades and credits: " << endl; // repeat until end of input while (cin >> grade >> credits) { points = getpoints(grade); totalpoints += points * credits; totalcredits += credits; printf("grade is: %-2s credits = %4f points = %4.1f \n", grade.c_str(), credits, points); } // while not EOF cout.precision(3); cout << "GPA is " << totalpoints / totalcredits << endl; return 0; // 0 means no errors } // main double getpoints(string& g) { double points = 0; g[0] = toupper(g[0]); switch (g[0]) { case 'A': points = 4; break; case 'B': points = 3; break; case 'C': points = 2; break; case 'D': points = 1; break; default: points = 0; } if (g.size() > 1) { if (g.at(1) == '+' && 'B' <= g[0] && g[0] <= 'D') points += .3; if (g.at(1) == '-' && 'A' <= g[0] && g[0] <= 'C') points -= .3; } return points; }