I am a beginner c++ student and I am having trouble understanding what i should do. I am tasked with writing a program that will read a file containing student exam, assignment, and project scores and output a letter grade for each student, by last name. The output will go into a seperate text file the user will name.
The input text file will have the following format:
Smith Jan Assignment 30
Smith Jan Exam 78
May Sean Assignment 18.5
May Sean Exam 88
Smith Jan Exam 89.3
May Sean Assignment 18
May Sean Exam 100
Smith Jan Project 50
May Sean Project 50
The output text file will have the following format:
Sean May A
Jan Smith B
We are using structs and vectors for this assigment. I am not understanding how to read in from my text file into the struct, and also how to make each students' scores go into their own vector, so i can apply my function to calculate the grade average and letter grade. I know how to do the grading part, but this is the part that confuses me. I have tried to read and research in many books and resources, but its hard to understand just by reading that stuff. I learn much better if i can see examples in action. Any help is appreciated. Here is the code that I have so far:
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace std;
// structured data type containing information on students
struct Student_data
{
// member variables
string first_name;
string last_name;
double assignment;
double exam;
double project;
};
int main()
{
// get the input file name from the user
string filename;
cout << "Enter the input filename: ";
getline(cin, filename);
string lname;
string fname;
// try to open the input file
ifstream infile{filename};
if (!infile)
{
cout << "Error: could not open input file\n";
}
else
{
// get the output file name from the user
cout << "Enter the output filename: ";
getline(cin, filename);
// try to open the output file
ofstream outfile{filename};
if (!outfile)
{
cout << "Error: could not open output file\n";
}
else
{
// read in the data from the input file and output it
// to the output file
// read in first line of document
infile >> lname;
infile >> fname;
}
}
cout << fname << " "<< lname <<"\n";
}
Line 38 on is a different approach. Myself I like to open and check each file open separately. It makes it easier to find errors.
On line 49 I will suggest std::ofstream(filename, std::ios::trunc | std::ios::ate); the trunc will empty the file and ate will put the file pointer at the end of the file. This keeps the output file small while writing the program and makes it easier to see what is happening.
Lines 62 and 63 need to move to line 67 where they should be in a while loop to read and process the entire file. The infile >> lname; should look something like infile >> variableNameOfStruct.lname; and similiar for the other variables of the struct. Something else I would do is to create a small function to initialize or zero the struct. This way you can call the function after the instance of the struct is defined or after you process each read from the input file. it saves on the headache of trying to figure out why you have a value in a variable that should not be there.
Now for the vector I am thinking of a vector of structs. This way each element of the vector is a complete struct with different information. You can use either subscript notation or an iterator.