Created
April 15, 2014 22:10
-
-
Save AdamJHowell/10782116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <iomanip> | |
#include <string> | |
#include <vector> | |
#include <cstdlib> | |
using namespace std; | |
struct Student{ | |
string name, status; | |
double grade1, grade2, | |
grade3, average; | |
}; | |
string getInputFileName(); | |
string getOutputFileName(); | |
void readStudents(vector<Student>&, string); | |
void writeStudents(vector<Student>&, string); | |
int main(){ | |
vector<Student> studentVector; | |
string inputFileName, outputFileName; | |
inputFileName = getInputFileName(); | |
outputFileName = getOutputFileName(); | |
readStudents(studentVector, inputFileName); | |
writeStudents(studentVector, outputFileName); | |
system("pause"); | |
return 0; | |
} | |
string getInputFileName(){ | |
string getLocation; | |
cout << "Please type the location of the file" | |
<< " you would like to open." << endl | |
<< ">>"; | |
cin >> getLocation; | |
return getLocation; | |
} | |
string getOutputFileName(){ | |
string writeLocation; | |
cout << "Please type the location of the file" | |
<< " you would like to write to." << endl | |
<< ">>"; | |
cin >> writeLocation; | |
return writeLocation; | |
} | |
void readStudents(vector<Student>& _studentVector, string _inFileName){ | |
Student tempStudent; | |
ifstream inFile; | |
string fileStr = ""; | |
inFile.open(_inFileName); | |
// Test code. | |
cout << "Reading file " << _inFileName << endl; | |
while(!inFile.eof()){ | |
// Read each line into one string. | |
getline( inFile, fileStr, '\n' ); // The last part of this is the delimiter. Switch it from '\n' to ' ', and see how it behaves. | |
// inFile >> tempStudent.name; // This input method will not work. | |
// inFile >> tempStudent.grade1; | |
// inFile >> tempStudent.grade2; | |
// inFile >> tempStudent.grade3; | |
// inFile >> tempStudent.average; | |
// inFile >> tempStudent.status; | |
// Test code. | |
cout << "Read in:\n" << fileStr << endl; | |
// Now you need to send each part of fileStr to your student vector. | |
// Use split to divide fileStr into separate parts. | |
tempStudent.name = fileStr; // This sends the entire string to just the name portion of tempStudent. | |
// Push the completed tempStudent into _studentVector. | |
_studentVector.push_back( tempStudent ); | |
} | |
inFile.close(); | |
} | |
void writeStudents(vector<Student>& _studentVector, string _outFileName){ | |
Student tempStudent; | |
ofstream outFile; | |
outFile.open(_outFileName, ofstream::out); | |
// Test code. | |
cout << "Writing file " << _outFileName << endl; | |
for (int i = 0; i < _studentVector.size(); i++) | |
{ | |
// Test code. | |
//cout << "Writing: " << tempStudent.name << tempStudent.grade1 << tempStudent.grade2 << tempStudent.grade3 << tempStudent.average << tempStudent.status << endl; | |
// This last part of the for() loop is not working properly, but I don't have time to troubleshoot it. | |
tempStudent = _studentVector.at(i); | |
outFile << tempStudent.name; | |
outFile << tempStudent.grade1; | |
outFile << tempStudent.grade2; | |
outFile << tempStudent.grade3; | |
outFile << tempStudent.average; | |
outFile << tempStudent.status; | |
} | |
outFile.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment