-1

I was wondering if anyone could help me with my code (C++)? This is the function that will not work. When I run it, Windows pops up and says "This program has stopped working." Here is the code that will not run (it's a little long, but the program is proprietary and I appreciate any help):

void ClassName::loadGrades(){
    string temp;
    int count=0;

    ifstream labErnIn("labGradesErn.txt");
    if(labErnIn.is_open()){
        count=0;
        while(labErnIn.good()){
            getline(labErnIn, temp);
            float tempF = ::atof(temp.c_str());
            labGradesErn[count] = tempF;
            if(labGradesErn[count]==0 && count==0) labGradesErn[count]=-1;
            count++;
        }
        labGradesErn[count-1]=-1;
        labErnIn.close();
    }

    else{
        cout << "Unable to open file labGradesErn.txt" << endl;
    }
    // I repeat this for three other sections, same code, different var names.
    // From 'ifstream...' to 'Unable to open file'
}

All variables not declared in this function are declared elsewhere.

Thanks, I really appreciate any and all help!

mkel23
  • 115
  • 1
  • 7
  • 2
    So naturally, you fired up your debugger an discovered that it stopped working because... – StoryTeller - Unslander Monica Feb 11 '13 at 00:39
  • Using eclipse, nothing was found wrong. I think there's something wrong with the way Windows handles it that I can't seem to find. Not really sure. Sorry, I am new at C++. – mkel23 Feb 11 '13 at 00:41
  • **[`while(labErnIn.good())` is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong).** What learning resource told you to do that? – Lightness Races in Orbit Feb 11 '13 at 00:42
  • If I had to guess, I'd say that you created `labGradesErn` wrong, but (a) there's no way we can tell what's wrong from this code, which is not in itself incorrect other than in the sense that I've already mentioned, and (b) this is a localised "debug my code please" post, not a SO question. – Lightness Races in Orbit Feb 11 '13 at 00:44
  • @LightnessRacesinOrbit I checked the C++ resource website. http://www.cplusplus.com/doc/tutorial/files/ Here, it uses this. I thought this was to make sure the file was not corrupt? Is there something else I should be doing? – mkel23 Feb 11 '13 at 00:51
  • @MikeTheTike - Yes, there is something else you should be doing. In your case, your loop should be `while(getline(labErnIn, temp)) { ... }` – Robᵩ Feb 11 '13 at 01:12
  • If your strange loop does 0 iterations, you end up asigning `labGradesErn[-1]`, and that will certainly give you grief. The strange handling of the `count == 0` case should perhaps be moved out of the loop? – vonbrand Feb 11 '13 at 01:35
  • Code looks ok to me. Maybe do a check on `labGradesErn[count-1]` so you don't access negative index. Also check OS file permission, ensure the user running the code has permission to read the file. Try running the code as administrator if you're on windows? – gerrytan Feb 11 '13 at 01:39
  • 4
    @MikeTheTike: **cplusplus.com is _not_ "the C++ resource website".** That tutorial, and the others on the website, are _not recommended_. Prefer [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Lightness Races in Orbit Feb 11 '13 at 12:01
  • 3
    http://kera.name/articles/2013/02/cplusplus-com-is-bad-and-you-should-feel-bad/ – Lightness Races in Orbit Feb 11 '13 at 12:14

1 Answers1

1

If labGradesErn is a fixed array, you risk an array overrun sooner or later. If it is a std::vector, you should use push_back() to append elements, because increasing the index doesn't increase the vector.

Another point is your while loop. You don't test if getline succeeds and you should loop on getline instead of good

while (getline(labErnIn, temp)) {
...
}
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thank you. It was with `good`. My friend found the error. Nothing with the debugger or the code itself. – mkel23 Feb 11 '13 at 02:57