vendredi 22 juillet 2016

Vector duplicates the value

Firstly be mercyful, i'm a beginner in C++.

I wrote this code for my interpreter: Reading a line from source and splitting line to words. I using a vector object for storing words. Here is the code, Source is file descriptor (ifstream):

#define __TEST__ 1
string Line; vector<string> Words; string Word; while(getline(Source, Line)){ 
    for(unsigned long Index = 0; Index <= Line.length(); Index++){
        if(Line[Index] == '\40' or Line[Index] == '\0'){ // "\40" = " "
            Words.push_back(Word); //Inject the Word to Words
            Word.clear();
        }else{
            Word += Line[Index];
        }
    }
    #if __TEST__
        cout << Words[0] << "\n";
    #endif
    //Interpration starts here
    Words.empty();  
}

And this is the file interpreted by program:

10 * 20 / 5 * 10
Asparagas

And this is the output:

10
10

Like you can see here, value is duplicated. What is the problem?

Aucun commentaire:

Enregistrer un commentaire