Hello,
I am trying to read a data file into a vector and print the values through class. When compiled there are no errors, but the program just stops after opening the file. Can't seem to figure out the problem. Any help is appreciated. Thank you.
put a cout << t << endl; on line 34 so it proves you opened the file, got data, and put something into that vector. if that prints the file contents correctly, we can look deeper.
my personal guess is that the above does not work because time() is empty so time.size() is 0
void readfile() {
while(infile >> t) {
for (int i = 0; i < time.size(); i++) {
time.push_back(t);
}
}
}
I'm not sure what you're trying to accomplish here, but nothing will ever be added to time. At the start, time.size() is 0, i is 0 which is not < 0 so .push_back() will never be executed!
Perhaps (not tried) ?
1 2 3
void readfile() {
for (double t {}; infile >> t; time.push_back(t));
}
Also for print():
1 2 3 4
void print() {
for (constauto& t : time)
cout << t << '\n';
}