0

Using getline to store information, I want to have an array which store a whole column in a text file, using a '/' as the delimiter, but when creating a loop which goes through the first line and stores that in a[i] etc, then moves onto the next line.`

const int MAX = 20;
int main(){

    string menuname;
    string a[MAX];
    string d[MAX];
    string b[MAX];
    string c[MAX];
    string line;
    bool error = false;
    ifstream openFile;
    int counter = 0;

    do{
        cout  << "Please enter the name of the menu you would like to open: ";
        cin >> menuname;
        menuname +=  ".txt";
        openFile.open(menuname.c_str());
        if(openFile.fail()){
            cerr << "Unable to open file, please re-enter the name.\n";
            error = true;
        }
    //Determine how many lines long the text file is
        while(getline(openFile, line)){
            ++counter;
        }
       //Testing the counter
    cout << counter;
    }while(error == true);

    while(! openFile.eof()){
         for(int i = 0; i < counter; i++){
            getline( openFile, a[i], '/');
            getline( openFile, b[i], '/');
            getline( openFile, c[i], '/');
            getline( openFile, d[i]);
        }
    }
    for(int i = 0; i < counter; i++){
        cout << a[i] << b[i];
    }
}

There is currently no errors when i run the program, and I have tested the counter variable by just showing an output, which is working correctly, but at the bottom of the program I have created a small test which should print some 2 of the arrays I store, but it prints nothing and the program just ends after displaying the value of counter.

4
  • Provide a minimal reproducible example please. Include any error messages (compile time or runtime) into your question. For linker errors provide your linker command line please (otherwise if you even don't know refer to What is an undefined reference/unresolved external symbol error and how do I fix it? 1st). Also provide us with your observations for runtime errors, when you run your code in the debugger and stepping through every single line. Commented May 4, 2016 at 18:10
  • You want to store a whole line in array a ? Or a single character ? Bcoz your are reading using getline and passing a single character to store the value . Commented May 4, 2016 at 18:15
  • The textfile is in a format of xxx/xxx/xxx/xxx, with multiple rows, I want an array to store a whole column each Commented May 4, 2016 at 18:16
  • @AbhishekPanjabi That is incorrect. a is an array of std::strings so a[i] is a single string which is what getline needs. a[i][j] would be a single character. Commented May 4, 2016 at 18:21

1 Answer 1

3

The problem is you are at the end of the file when you go to actually store the data.

while(getline(openFile, line)){
    ++counter;
}

Reads the file all the way to the end and then sets the EOF flag on the string. Then you get to

while(! openFile.eof()){
     for(int i = 0; i < counter; i++){
        getline( openFile, a[i], '/');
        getline( openFile, b[i], '/');
        getline( openFile, c[i], '/');
        getline( openFile, d[i]);
    }
}

And since the EOF flag is set the while loop is never executed. Since all you really need counter for is the the display loop at the end we can combine the counter loop and the reading loop into one loop like

while(getline( openFile, a[counter], '/') && getline( openFile, b[counter], '/') &&
      getline( openFile, c[counter], '/') && getline( openFile, d[counter])){
    counter++;
}

And now we read in the full file and get a count of the number of lines read.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried removing the original counter loop, and the for loop with while(! openfile.eof()){, then using the new while loop you suggested, but still am getting nothing output on the screen.
@Thecube Is this how your code looks now?
Code looks the same, while(getline( openFile, a[i], '/') && getline( openFile, b[i], '/') && getline( openFile, c[i], '/') && getline( openFile, d[i])){ counter++; } has a problem where i has not be declared, which is where i used a for loop previously
@Thecube durp. I got rid of i. all those instances of i should be counter. I updated the answer.
Was looking through code and couldn't figure what I hadn't change, it is now working thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.