views:

121

answers:

3

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]

A: 
printLowestRecord("gradebook.txt");
sortFileRecords("gradebook.txt", "sortedGradebook.txt");

Maybe you need to specify the full absolute path to the file?

Chad
A: 
printLowestRecord("gradebook.txt");
sortFileRecords("gradebook.txt", "sortedGradebook.txt");

In those two lines, try writing the complete filepath for the output text. I'm thinking that's the problem.

Sergio Tapia
It's not that, as the file "sortedGradebook.txt" just shows "Jone" and that's it.
Anthony Glyadchenko
+2  A: 

The code seems to be correct so far, I think your test data is wrong. If I test with this input file:

a 10 c
d 2 f
g 9 i
j 4 l
m 8 o
p 6 r
s 7 u
v 8 x
y 6 a
b 10 d
e 5 g
h 12 j

The output file is like this, which is the expected behaviour:

d 2 f
j 4 l
e 5 g
y 6 a
p 6 r
s 7 u
m 8 o
v 8 x
g 9 i
b 10 d
a 10 c
h 12 j

So either your test data is wrong or there is some additional error handling you have to do (file can't be opened etc.).

By the way, this part of your code

                            else if (firstSpace != -1 && secondSpace == -1){

can be reduced to

                            else {

because you have a break statement in there and set secondSpace back to -1 right after it.

EDIT: Your data also works fine - output is this:

Jone 75 [email protected]
Alice 78 [email protected]
Nick 79 [email protected]
Sean 80 [email protected]
Mark 86 [email protected]
Thomas 88 [email protected]
Zach 89 [email protected]
Claire 89 [email protected]
Amy 95 [email protected]
Eve 97 [email protected]
Issac 99 [email protected]
James 100 [email protected]
schnaader
Yes, the code is fine, I've checked it with similar results. Ergo the problem you is an environmental one. Does the file exist, does it exist where your program is running, and is it of the right format?
paxdiablo
It exists and it is a text file.
Anthony Glyadchenko
@Anthony, we need your input file.
paxdiablo
Works fine here, there must be something wrong with your filenames, environment or similar. You could also try to delete the executable and recompile just to make sure you're using the right version.
schnaader
Please note that I'm using Xcode on a Mac. Is there any way that I could make it work on the Mac?
Anthony Glyadchenko
Concur on the output. It's possibly interesting that Jone is the first in the list and, according to OP, the only one that comes out.
paxdiablo
Since this works fine under Cygwin (for me) and (probably) VC++ for @schnaader, I would suggest this is a Mac/Xcode problem - maybe a retag would help.
paxdiablo
In addition, start peppering your code with debug statements to see if you can find the problem (or, even better if available, use a single-stepping debugger).
paxdiablo
I tested with G++ 3.4.5, I agree that this is some Mac (file system/permission/flushing...)/XCode (compiler bug/environmental settings...) problem.
schnaader