Running Test 1 ../resource/scripts/run.sh: line 5: ./a.out: No such file or dire
ID: 3734094 • Letter: R
Question
Running Test 1 ../resource/scripts/run.sh: line 5: ./a.out: No such file or dire 0al,15 > Enter a course name: CMPSC 101 > Enter number of credits: 3 > Enter your grade (A, B, C, D, F): A s Continue ('Yes' or No? Yes > Enter a course name: BIO 100 > Enter number of credits: 3 > Enter your grade (A, B, C, D, F): B > Continue ('Yes' or 'No')? Yes > Enter a course name: ACCT 50 > Enter number of credits: 1 > Enter your grade (A, B, c, D, F): D > Continue ('Yes' or 'No')? No > Total grade points: 22 > Total credits attempted: 7 > Your GPA is 3.14 Test 1 failed ccc_vl w e3a4c 23050runweb8:-$Explanation / Answer
Solution to given problem is provided below. Comments are added in code for explanatory purpose. Sample execution output is also given below.
File: gpa.cpp
#include <iostream>
#include <iomanip>
using namespace std;
// Return grade points for given grade
float convertGradeToGradePoints(char grade) {
switch (grade) {
case 'A':
return 4.00;
case 'B':
return 3.00;
case 'C':
return 2.00;
case 'D':
return 1.00;
case 'F':
default:
return 0.00;
}
}
int main() {
string courseName;
int numCredits;
char grade;
string repeat;
int totalCredits = 0;
float totalGradePoints = 0;
float gpa;
do {
// Read course name
cout << "Enter a course name: ";
cin >> courseName;
// Ignore until new line
cin.ignore(256,' ');
// Read number of credits
cout << "Enter number of credits: ";
cin >> numCredits;
// Read grade
cout << "Enter your grade (A, B, C, D, F): ";
cin >> grade;
// Read if student want to add more courses
cout << "Continue ('Yes' or 'No')? ";
cin >> repeat;
// Update total credits and total grade points
totalCredits += numCredits;
totalGradePoints += numCredits * convertGradeToGradePoints(grade);
} while (repeat.compare("Yes") == 0);
// Compute GPA
gpa = totalGradePoints / totalCredits;
// Output total grade points, total credits and GPA
cout << "Total grade points: " << std::setprecision(0) << std::fixed << totalGradePoints << endl;
cout << "Total credits attempted: " << totalCredits << endl;
cout << "Your GPA is " << std::setprecision(2) << std::fixed << gpa << endl;
return 0;
}
Sample Execution Output:
$ g++ gpa.cpp
$ ./a.out
Enter a course name: CMPSC 101
Enter number of credits: 3
Enter your grade (A, B, C, D, F): A
Continue ('Yes' or 'No')? Yes
Enter a course name: BIO 100
Enter number of credits: 3
Enter your grade (A, B, C, D, F): B
Continue ('Yes' or 'No')? Yes
Enter a course name: ACCT 50
Enter number of credits: 1
Enter your grade (A, B, C, D, F): D
Continue ('Yes' or 'No')? No
Total grade points: 22
Total credits attempted: 7
Your GPA is 3.14
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.