I've debugged my program and the arrays seem to be allocated well. However for some strange and stupid reason, the code doesn't output the arrays into the file.
Please help me spot my bug or such!
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
void sRecSort(string *n, int *s, string *e, int len){
for (int i = 0; i < len; i++){
for (int j = i + 1; j < len; j++){
if (s[j] < s[i]){
swap(s[i],s[j]);
swap(e[i],e[j]);
swap(n[i],n[j]);
}
}
}
}
void printLowestRecord(char inFileName[]){
string tempSubString = " ";
string names[12] = {" "};
int grades[12] = {0};
string emails[12] = {""};
int firstSpace = -1;
int secondSpace = -1;
ifstream inputMe(inFileName);
while (!inputMe.eof()){
for (int i = 0; i < 12; i++){
getline(inputMe, tempSubString);
for (int w = 0; w < strlen(tempSubString.c_str()); w++){
if (tempSubString[w] != ' '){
continue;
}
else{
if (firstSpace == -1){
firstSpace = w;
}
else if (firstSpace != -1 && secondSpace == -1){
secondSpace = w;
names[i] = tempSubString.substr(0, firstSpace);
grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str());
emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1));
break;
}
}
}
firstSpace = -1;
secondSpace = -1;
}
}
sRecSort(names, grades, emails, 12);
inputMe.close();
}
void sortFileRecords(char inFileName[], char outFileName[]){
ifstream inputFile(inFileName);
ofstream outputFile(outFileName);
string tempSubString = " ";
string names[12] = {" "};
int grades[12] = {0};
string emails[12] = {" "};
int firstSpace = -1;
int secondSpace = -1;
while (!inputFile.eof()){
for (int i = 0; i < 12; i++){
getline(inputFile, tempSubString);
for (int w = 0; w < strlen(tempSubString.c_str()); w++){
if (tempSubString[w] != ' '){
continue;
}
else{
if (firstSpace == -1){
firstSpace = w;
}
else if (firstSpace != -1 && secondSpace == -1){
secondSpace = w;
names[i] = tempSubString.substr(0, firstSpace);
grades[i] = atoi((tempSubString.substr(firstSpace + 1, secondSpace - (firstSpace + 1))).c_str());
emails[i] = tempSubString.substr(secondSpace + 1, tempSubString.length() - (secondSpace + 1));
break;
}
}
}
firstSpace = -1;
secondSpace = -1;
}
}
sRecSort(names, grades, emails, 12);
for (int q = 0; q < 12; q++){
outputFile << names[q] << " " << grades[q] << " " << emails[q] << endl;
}
inputFile.close();
outputFile.close();
}
int main (int argc, char * const argv[]) {
printLowestRecord("gradebook.txt");
sortFileRecords("gradebook.txt", "sortedGradebook.txt");
return 0;
}
Here's my data:
Sean 80 [email protected]
James 100 [email protected]
Issac 99 [email protected]
Thomas 88 [email protected]
Alice 78 [email protected]
Jone 75 [email protected]
Zach 89 [email protected]
Mark 86 [email protected]
Nick 79 [email protected]
Amy 95 [email protected]
Claire 89 [email protected]
Eve 97 [email protected]