In this problem, you will be prompting a student for grades and credits for cour
ID: 3736272 • Letter: I
Question
In this problem, you will be prompting a student for grades and credits for courses taker From that, you will calculate a GPA. This site: http://www.back2college.com/gpa.htm shows you how to calculate a GPA. Keep prompting the student if they want to add more courses IMPORTANT NOTES! . The course name is prompted for, but nothing is done with it. . We are just using the grades A, B, C, D, and F so you won't have to do so much typing! . You will need to use the "set precision" command as shown in the book. Set it to "fixe and "2". . You will need to use the "cin.ignore0)" function as discussed earlier in the course.Explanation / Answer
//Include this header file while using visual studio.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
//Define the main function.
int main()
{
//Define the variables.
string cname;
int credits = 0;
char grade = 'A';
string ch = " ";
int totalcredits = 0;
double totalgrades = 0;
double totalg = 0;
double val = 0;
//Prompt the user for input
//until user enters no.
while (true)
{
//Prompt the user for input.
cout << "Enter course name:";
getline(cin, cname);
cout << endl;
cout << "Enter number of credits:";
cin >> credits;
cout << endl;
totalcredits += credits;
cout << "Enter your grade(A,B,C,D,E,F):";
cin >> grade;
cout << endl;
//Check for the grade entered by user
//Evaluate the value and update the totalgrades.
if (grade == 'A' || grade == 'a')
{
val = 4.00*credits;
totalgrades += val;
}
else if (grade == 'B' || grade == 'b')
{
val = 3.00*credits;
totalgrades += val;
}
else if (grade == 'C' || grade == 'c')
{
val = 2.00*credits;
totalgrades += val;
}
else if (grade == 'D' || grade == 'd')
{
val = 1.00*credits;
totalgrades += val;
}
else
totalgrades += 0;
//Prompt the user to continue.
cout << "continue(yes or no):";
cin >> ch;
cout << endl;
//check if the user enters no
//then exit otherwise countinue in the program.
if (ch == "no" || ch == "NO" || ch == "No")
{
//Dispaly the results
cout << "Total grade points:" << totalgrades<< endl;
cout << endl;
cout << "Total credits attempted:"<< totalcredits;
cout << endl;
//Set fixed precision of 2 digit.
cout << " Your GPA is:" << fixed<< setprecision(2)<< totalgrades / totalcredits;
cout << endl<< endl;
break;
}
//Flush the cin.
cin.ignore();
}
system("pause");
//Return the value zero
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.