1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <string>
#include <iomanip> // setw(), setfill(), etc.
#include <fstream> // input/output file stream
using namespace std;
// Constants for the array lengths
const int studentArr = 2;
const int gradesLength = 4;
struct studentInfo {
string fName;
string lName;
int g1;
int g2;
int g3;
int g4;
};
int main() {
int count = 0;
studentInfo arr[6]; // fname, lname, g1, g2, g3, g4
studentInfo grades[gradesLength]; // pos 0, 1, 2, 3 per student
string inFile, outFile;
cout << "Please enter an input file name: ";
cin >> inFile;
ifstream fin; // fin = fileInput
fin.open(inFile); // inFile = holds input file name
// Checks if file has opened successfully. If not it will do this:
if (fin.fail()) {
cout << "Couldn't find the given file." << endl;
exit(0); // does this stop the program?
}
else { // If file has opened correctly, it will do this:
// Program should read numbers of student records into an array.
while (!fin.eof()) { // While it has not reached the end of the input file..
// fin is like cin but for input files. Will allow us to output the text later.
fin >> arr[count].fName >> arr[count].lName >>
/*
The "arr" array stores all of the info (fName, lName, g1, g2, g3, g4)
count will make sure all info from the read file is stored into the array.
From my understanding, this array has multiple sections. Like so:
Array Structure:
fName
- pos 0: John
- pos 1: Amy
- pos 2: Carol
lName
- pos 0: Doe
- pos 1: Kargol
- pos 2: Johnstone
g1
- pos 0: 90 (John Doe's first test grade)
- pos 1: 80 (Amy Kargol's first test grade)
- pos 2: 70 (Carol Johnstone's first test grade)
g2
- pos 0: 55 (John Doe's 2nd test grade)
- pos 1: 82 (Amy Kargol's 2nd test grade)
- Etc...
Etc...
*/
arr[count].g1 >> arr[count].g2 >> arr[count].g3 >>
arr[count].g4;
count++; // acts as an i would in a for loop.
}
// Closing input file
fin.close();
// Opening output file
cout << "Enter the output file name: ";
cin >> outFile;
ofstream fout;
fout.open(outFile);
if (!fout.fail()) { // if the output file has been opened successfully..?:
// Writes to output file
fout << setfill('=') << setw(78) << endl;
cout << 77 << endl;
// Closing output file
fout.close();
}
}
// General Stuff
cin.ignore(1000, '\n');
cin.get();
return 0;
}
|