Question

Hello I need help fixing my C++ code. I need to display in the console the information saved in the file as well have the content saved in the file output in a fixed position like the screenshot. Thanks.

CODE

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

//main function
int main()
{
   //variable to store student id
   int id;
  
   //variables to store old gpa(ogpa), old course credits(occ), new course credits(ncc), current gpa(cur_gpa), cumulative gpa(cum_gpa)
   float ogpa, occ, ncc, cur_gpa, cum_gpa;
  
   //variables to store course credits
   float c1, c2, c3, c4;
  
   //variables to store grades
   float g1, g2, g3, g4;
  
   //variables to store old honor points and new honor points
   float ohp, nhp;
  
   //variables for input file name and output file name
   char in_file_name[100],out_file_name[100];
  
   //objects for read from file and write to file
   ifstream in_stream;
   ofstream out_stream;
  
   //prompt for input file name
   cout << "Enter the input file name: ";
   cin >> in_file_name;
  
   //prompt for output file name
   cout << "Enter the output file name: ";
   cin >> out_file_name;
  
   //opening input file
   in_stream.open(in_file_name);
  
   //reading student id, old gpa, old course credits
   in_stream >> id >> ogpa >> occ;
  
   //reading course credits
   in_stream >> c1 >> c2 >> c3 >> c4;
  
   //reading grades
   in_stream >> g1 >> g2 >> g3 >> g4;
  
   //calculating old honor points
   ohp = occ * ogpa;
  
   //calculating new honor points
   nhp = (c1 * g1) + (c2 * g2) + (c3 * g3) + (c4 * g4);
  
   //calculating new course credits
   ncc = c1 + c2 + c3 + c4;
  
   //calculating current gpa
   cur_gpa = nhp / ncc;
  
   //calculating cumulative gpa
   cum_gpa = (nhp + ohp) / (ncc + occ);
  
   //opening output file
   out_stream.open(out_file_name);
  
   //writing student id to file
   out_stream << "Student ID number:\t\t\t\t" << id << endl;
  
   //writing old course credits to file
   out_stream << "Previous semester's credit:\t\t" << occ << endl;
  
   //writing old gpa to file
   out_stream << "Previous semester's GPA:\t\t" << ogpa << endl;
  
   //writing current gpa to file
   out_stream << "GPA of current semester:\t\t" << fixed << setprecision(1) << cur_gpa << endl;
  
   //writing cumulative gpa to file
   out_stream << "Cumulative GPA:\t\t\t\t\t" << fixed << setprecision(1) << cum_gpa << endl;
  
   //writing total credits to file
   out_stream << "Total credits completed:\t\t" << fixed << setprecision(0) << (occ + ncc) << endl;
  
   //closing input file
   in_stream.close();
  
   //closing output file
   out_stream.close();
}

Screenshot:

Write a program to calculate the current semesters grade point average and the cumulative grade point average of a student.

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

Dear student copy and past the below code and run you will get the output in correct formate

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

//main function
int main()
{
//variable to store student id
int id;
  
//variables to store old gpa(ogpa), old course credits(occ), new course credits(ncc), current gpa(cur_gpa), cumulative gpa(cum_gpa)
float ogpa, occ, ncc, cur_gpa, cum_gpa;
  
//variables to store course credits
float c1, c2, c3, c4;
  
//variables to store grades
float g1, g2, g3, g4;
  
//variables to store old honor points and new honor points
float ohp, nhp;
  
//variables for input file name and output file name
char in_file_name[100],out_file_name[100];
  
//objects for read from file and write to file
ifstream in_stream;
ofstream out_stream;
  
//prompt for input file name
cout << "Enter the input file name: ";
cin >> in_file_name;
  
//prompt for output file name
cout << "Enter the output file name: ";
cin >> out_file_name;
  
//opening input file
in_stream.open(in_file_name);
  
//reading student id, old gpa, old course credits
in_stream >> id >> ogpa >> occ;
  
//reading course credits
in_stream >> c1 >> c2 >> c3 >> c4;
  
//reading grades
in_stream >> g1 >> g2 >> g3 >> g4;
  
//calculating old honor points
ohp = occ * ogpa;
  
//calculating new honor points
nhp = (c1 * g1) + (c2 * g2) + (c3 * g3) + (c4 * g4);
  
//calculating new course credits
ncc = c1 + c2 + c3 + c4;
  
//calculating current gpa
cur_gpa = nhp / ncc;
  
//calculating cumulative gpa
cum_gpa = (nhp + ohp) / (ncc + occ);
  
//opening output file
out_stream.open(out_file_name);
  
//writing student id to file
out_stream << "Student ID number: " << id << endl;
  
//writing old course credits to file
out_stream << "Previous semester's credit: " << occ << endl;
  
//writing old gpa to file
out_stream << "Previous semester's GPA: " << ogpa << endl;
  
//writing current gpa to file
out_stream << "GPA of current semester: " << fixed << setprecision(1) << cur_gpa << endl;
  
//writing cumulative gpa to file
out_stream << "Cumulative GPA: " << fixed << setprecision(1) << cum_gpa << endl;
  
//writing total credits to file
out_stream << "Total credits completed: " << fixed << setprecision(0) << (occ + ncc) << endl;
  
//closing input file
in_stream.close();
  
//closing output file
out_stream.close();
}

output:

output - Notepad File Edit Format View Help Student ID number: Previous semesters credit: Previous semesters GPA: GPA of cu

Add a comment
Know the answer?
Add Answer to:
Hello I need help fixing my C++ code. I need to display in the console the...
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
  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • NEED HELP WITH MY C++ ASSIGNMENT, IT NEEDS TO BE SOLVED USING LISTS #include <iostream> #include...

    NEED HELP WITH MY C++ ASSIGNMENT, IT NEEDS TO BE SOLVED USING LISTS #include <iostream> #include <list> #include <map> #include <string> using namespace std; class course { public: string name; int section; int credits; course() {} course(string n, int s, int c) { name = n; section = s; credits = c; } //Add additional needed member functions and implement them. //You also need to implement some needed functions for overloading operator<< . }; //Implement the following functions void add_student(map<int,...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • Please help me to write a program in C++ with comments and to keep it simple...

    Please help me to write a program in C++ with comments and to keep it simple as possible. Also, post the output please. Thank you! Here is input data: 2333021 BOKOW, R. NS201 3 A 2333021 BOKOW, R. MG342 3 A 2333021 BOKOW, R. FA302 1 A 2574063 FALLIN, D. MK106 3 C 2574063 FALLIN, D. MA208 3 B 2574063 FALLIN, D. CM201 3 C 2574063 FALLIN, D. CP101 2 B 2663628 KINGSLEY, M. QA140 3 A 2663628 KINGSLEY, M....

  • C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications...

    C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications Included is an input file. Included is a function to aide, if needed Use the functions, enum, and namespace defined in Assignment 6. You will need to use a loop to read each student’s data You will need to load every homework grade into an array Calculate the homework average The homework average can be calculated while the array is loaded Move the printing...

  • Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the...

    Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the prototype added to the .h file......and the implementation in the .cpp file....thanks! edited: I just need the above and then I was told to use the methods in the main program; for the vehicle. Any 3 attributes and any three methofds. source.cpp file // Header Comment #include #include #include #include "Automobile.h" using namespace std; struct Address{ string street; string city; string state; string zip;...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

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