Question

Modify the GPA Calculation program so that you input your grades from a file, Grades.txt. To...

Modify the GPA Calculation program so that you input your grades from a file, Grades.txt. To include file I/O to your program, you must complete the following five steps: 1. Include the header file fstream in the program. 2. Declare file stream variable, ifstream inFile; 3. Associate the file stream variable with the I/O source. 4. Use the file stream variable with the extractor variable >> to get input. 5. Close the file. Use Dev c++ Visual Basic is not working

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

/**

This program calculates the GPA of a student

INPUT: A file containing grades and credit hours separated by comma

PROCESSING: A method getCreditPoints(char grade, int credit hours) which returns the corresponding total credit points (credit points for the grade * total credit hours for the course) associated with a particular grade

*/

#include <iostream>

#include <iomanip>

#include <fstream>

#include <sstream>

#include <cstdlib>

#include <vector>

using namespace std;

// function prototype

double getCreditPoints(char, int);

int main()

{

ifstream inFile("Grades.txt");

double totalCreditPoints = 0.0, GPA = 0.0;

int totalCreditHours = 0;

string line;

if(!inFile.is_open())

{

cout << "Cannot open file Grades.txt\n";

exit(0);

}

while(getline(inFile, line))

{

stringstream ss(line);

vector<string> tokens;

string token;

while(getline(ss, token, ','))

{

tokens.push_back(token);

}

char grade = tokens[0][0];

int creditHours = stod(tokens[1]);

totalCreditHours += creditHours;

double creditPoints = getCreditPoints(grade, creditHours);

totalCreditPoints += creditPoints;

cout << "Grade of = " << grade << " having credit hours of " << creditHours << " results to credit points of = " << creditPoints << endl;

}

inFile.close();

GPA = (totalCreditPoints / totalCreditHours);

cout << "\nThe GPA is = " << setprecision(2) << fixed << GPA << endl;

return 0;

}

double getCreditPoints(char grade, int creditHours)

{

double points = 0;

grade = toupper(grade);

if(grade == 'A')

points = (4.00 * creditHours);

else if(grade == 'B')

points = (3.00 * creditHours);

else if(grade == 'C')

points = (2.00 * creditHours);

else if(grade == 'D')

points = (1.00 * creditHours);

return points;

}

*********************************************************** SCREENSHOT *****************************************************

CODE SNIPPET SCREENSHOT :

CONSOLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Modify the GPA Calculation program so that you input your grades from a file, Grades.txt. To...
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
  • // Retire.cpp : Defines the entry point for the console application. // program to convery years...

    // Retire.cpp : Defines the entry point for the console application. // program to convery years to retire // sumin Kang // 9/26/17 #include "stdafx.h" #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; int main() { //declare files ifstream inFile; ofstream outFile; // declare constants and variables string name; int age; int ageInRetire; // open files inFile.open("D:\\retire"); outFile.open("D:\\retire"); // get input from user cout << "What is your name?"; getline(inFile, name); cout << "How old are you?";...

  • Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...

    Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension). No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a...

    Modify your PredictGPA.java file from Lab 2 to create a method called GPA that takes a string argument (the grade) and returns the corresponding value (ie 4.0 for an A, 3.33 for a B+, etc). Use this method call to reduce the “redundant” code that you had in Lab 2. The program must otherwise still run correctly as to the directions in Lab 2 and compute the GPA after using the method calls to determine the appropriate grade values. Trying...

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

  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

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