0

I have this problem and need really fast solving. It's my code:

void stypendium() {
        string tresc = "";
        string temp;
        stringstream ss;
        fstream file;
        vector<float> grades;
        float grade, sum, av;
        file.open(this->plik, ios_base::in);
        if(!file.is_open()) {
            ofstream fileTmp(this->plik);
            fileTmp.close();
            file.open(this->plik, ios_base::in);
        }
        while (file >> temp) {
            if(strspn( temp.c_str(), "-.0123456789" ) == temp.size()) {
                ss.str("");
                ss << temp;
                ss >> grade;
                grades.push_back(grade);
            }
        };
        sum = 0;
        for(int i=0;i<grades.size();++i) {
            sum += grades[i];
        }
        av = sum / grades.size();
        cout << sum << "/" << grades.size() << "=" << av;
        file.close();
    }

};

Problem is that in line

ss << temp;

after first iteration nothing gets to the stream, though the temp has value

I might even say it's after second iteration, because first iteration gets the word and doesn't go into if statement, the second gets number and works with it properly. Next iterations gets temp value properly, but nothing gets to the stream.

  • So, you use stringstream to convert string to float? Move ss declaration to the beginning of while block. Possibly related: http://stackoverflow.com/questions/5288036/how-to-clear-ostringstream – Alex F Apr 08 '14 at 10:35
  • Yes, it's for the programming lab to practise streams. Anyway it solved the problem, thank you very much for help!! – user3510363 Apr 08 '14 at 10:38
  • 1
    @user3510363 Please review your first post from about one hour ago. [Streams dont work as expected](http://stackoverflow.com/questions/22933580/streams-dont-work-as-expected/22934777#22934777). – CPlusPlus OOA and D Apr 08 '14 at 11:08

0 Answers0