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.