Hi everyone, I am having a difficulty with some code and I hope here somebody who is more expreinced from me can help me.
I am having a program that reads a file store the data as objects in a vector.
For example in the file i have :
Once upon a time in the west?#1#1#1#1#2# which is my object called Question with fields description, answerA, answerB, answerC, answerD, difficulty.
i am reading the data from the file like this :

    string str[6];
    string end="";
    int flevel=0; //variable to type_cast the dificulty
    input_file.clear();
    input_file.seekg(ios_base::beg);
    while (input_file.good())
    {
        getline(input_file,str[0],'#');
        getline(input_file,str[1],'#');
        getline(input_file,str[2],'#');
        getline(input_file,str[3],'#');
        getline(input_file,str[4],'#');
        getline(input_file,str[5]);
        flevel=atoi(str[5].c_str());
        temp_question->Add_data(flevel,str[0],str[1],str[2],str[3],str[4]);
        //add data in the object
        fdata->push_back(*temp_question);
        //add the object in the list with questions
    }

after that my data is in the my vector , where i can work with it.
after i do my modifications i am saving it to the same file like that :

ofstream output(filename);
    if(output.is_open())// check  if file is open
    {
        vector<Question>::iterator it;
        for(it=fdata->begin();it!=fdata->end();it++)
        {
            it->SaveFile(output);
        }
        cout<<"\nData saved in succesfully saved in the file!\n";
    }
    else
    {
        cout<<"\nFailed to read Input file\n"<<endl;
    }


    output.close();

where SaveFile(output) is a function from my class and it does this :

foutput<<description<<'#'<<answerA<<'#'<<answerB<<'#'<<answerC<<'#'
<<answerD<<'#'<<difficulty<<endl;

So my problem is the next: When i start the program for first time i load this data and i have N questions(in my case i have only 7 so far in the file). After that i save it and when i start it again i have N+1 questions because the end of the file is a new line. This new question have all the data from the previos one exept for the description.
For example if my last question is
Once upon a time in the west?#1#1#1#1#2# and i have one new line more in the file
in the vector i have "Question"object that has all these field except the description which is that part="Once upon a time in the west?"
Can you help me how to repare that bug in my program?
Thank you in advance :)

It appears to me the structure of the data being added to vector is different than the file. You're putting flevel(str[5]) before str[0], if you're not parsing the data to account for that it will give you screwy results, when you save it.

Also it looks like the '#' at the end of the line, in your example data, might be unnecessary.

Edited 3 Years Ago by tinstaafl

I am sorry i gave wrong example. The example is like that "Once upon a time in the west?#1#1#1#1#2"
OK, i will try with ordering them into my edit function in another order.

I did that change but that is not the problem . This function add_data is doing that

void Question::Add_data(string fdescription,string fanswerA, string fanswerB,
                         string fanswerC, string fanswerD,int fdifficulty)
//add information in the question
{
    difficulty=fdifficulty;
    description=fdescription;
    answerA=fanswerA;
    answerB=fanswerB;
    answerC=fanswerC;
    answerD=fanswerD;
}

I put the data into the object and just after that i put the object in the vector.
My problem is that after i start for first time the program it is ok. But after i save in the file after the last question i have that new line from

foutput<<description<<'#'<<answerA<<'#'<<answerB<<'#'<<answerC<<'#'
    <<answerD<<'#'<<difficulty<<endl;

And after i read the file again i have one extra question.

Here is the correct example
my last question is :
Once upon a time in the west?#1#1#1#1#2

in the first executing of the program it is number 7, but after saving to the file i have one new line after that .
In the second execute i have this :
Question 7
Description: Once upon a time in the west?
Answer A:1
Answer B:1
Answer C:1
Answer D:1
Level: 2

and i have some strange new question in the vector
**Question 8
Description:
Answer A:1
Answer B:1
Answer C:1
Answer D:1
Level: 2
**
Because of that last new line in the file. So i think i have some problem with the reading of the file

    string str[6];
    string end="";
    int flevel=0; //variable to type_cast the dificulty
    while (input_file.good())
    {
        getline(input_file,str[0],'#');
        getline(input_file,str[1],'#');
        getline(input_file,str[2],'#');
        getline(input_file,str[3],'#');
        getline(input_file,str[4],'#');
        getline(input_file,str[5]);
        flevel=atoi(str[5].c_str());
        temp_question->Add_data(str[0],str[1],str[2],str[3],str[4],flevel);
        //add data in the object
        fdata->push_back(*temp_question);
        //add the object in the list with questions
    }

i did it like that and it works :) I hope it is a good way and if somebody is looking for something like this to help himself :)

 string str[6];
    string end="";
    int flevel=0; //variable to type_cast the dificulty
    while (input_file.good())
    {
        for(int i=0;i<6;i++)
        {
            str[i]="";
        }
        getline(input_file,str[0],'#');
        getline(input_file,str[1],'#');
        getline(input_file,str[2],'#');
        getline(input_file,str[3],'#');
        getline(input_file,str[4],'#');
        getline(input_file,str[5]);
        flevel=atoi(str[5].c_str());
        if(flevel>0)
        {
            temp_question->Add_data(str[0],str[1],str[2],str[3],str[4],flevel);
            //add data in the object
            fdata->push_back(*temp_question);
            //add the object in the list with questions
        }
    }

After every loop i reset the array of string to empty , and because i have only 12 levels of difficulty , i am doing a check if the int is>0 i am inputing the data in the vector.
Thaks for the help from the people that wrote me :) Yours things make me think a little where i can change to make it work correctly:)

This question has already been answered. Start a new discussion instead.