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.

Thecube
  • 71
  • 8
  • Provide a **[MCVE]** 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?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) 1st). Also provide us with your observations for runtime errors, when you run your code in the debugger and stepping through every single line. – πάντα ῥεῖ May 04 '16 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 . – Abhishek Panjabi May 04 '16 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 – Thecube May 04 '16 at 18:16
  • @AbhishekPanjabi That is incorrect. `a` is an array of `std::string`s so `a[i]` is a single string which is what `getline` needs. `a[i][j]` would be a single character. – NathanOliver May 04 '16 at 18:21

1 Answers1

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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 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 May 04 '16 at 18:38
  • @Thecube Is [this](http://coliru.stacked-crooked.com/a/3465d79242b88dff) how your code looks now? – NathanOliver May 04 '16 at 18:40
  • 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 May 04 '16 at 18:47
  • @Thecube durp. I got rid of `i`. all those instances of `i` should be `counter`. I updated the answer. – NathanOliver May 04 '16 at 18:49
  • Was looking through code and couldn't figure what I hadn't change, it is now working thanks for the help. – Thecube May 04 '16 at 18:59