J'ai débogué mon programme et les tableaux semblent bien affectés. Cependant, pour une raison étrange et stupide, le code ne sort pas les tableaux dans le fichier.Pourquoi ce code ne sortira-t-il pas dans un fichier?
S'il vous plaît aidez-moi à repérer mon bug ou tel!
#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;
}
Voilà mes données:
Sean 80 [email protected]
James 100 [email protected]
Issac 99 [email protected]
Thomas 88 [email protected] edu
Alice 78 [email protected]
Jone 75 [email protected]
Zach 89 [email protected]
Mark 86 [email protected]mail.com
Nick 79 [email protected] om
Amy 95 [email protected]
Claire 89 [email protected]
Eve 97 [email protected]
Anthony, s'il vous plaît donner votre fichier d'entrée de sorte qu'il peut être testé avec cela. @schnaader et moi-même avons confirmé que votre code fonctionne avec nos fichiers de test. – paxdiablo
Oui, je le seconderais. Si vous ne pouvez pas donner le fichier de test original, veuillez au moins donner un exemple inventé qui échoue. – schnaader
Les données d'entrée que vous avez données fonctionnent très bien pour moi (voir mon article). Voir aussi le commentaire sur ma réponse pour d'autres solutions possibles à votre problème. – schnaader