I am writing a program in Dev C++ to read values from a .txt file into a series of arrays. The program I have written so far compiles and starts to run, but on the output some of the values are wrong, and after several lines the programs crashes. I attempted to debug it, and the compiler suggests that the problem might be where I am reading the file into the program, but I can't figure it out.
#include<iostream>
#include<math.h>
#include<iomanip>
#include<fstream>
#include<string>
usingnamespace std;
int main(void){
string fileName=" ";
ifstream file;
int rows(0), termsOne[rows], termsTwo[rows], columns(0), limitOne(0);
int limitTwo(0), i(0);
double valueX[rows], sinX[rows], sinOne[rows], errorOne[rows];
double sinTwo[rows], errorTwo[rows];
cout<<"filename? >> ";
cin>>fileName;
file.open(fileName.c_str());
if(file.fail())
cout<<"File did not open properly.";
file>>rows>>columns>>limitOne>>limitTwo;
for(i=0;i<rows;i++){
file>>valueX[i]>>sinX[i]>>sinOne[i]>>termsOne[i]>>errorOne[i]
>>sinTwo[i]>>termsTwo[i]>>errorTwo[i];//I think the problem is here
cout<<valueX[i]<<" "<<sinX[i]<<" "<<sinOne[i]<<" "
<<termsOne[i]<<" "<<errorOne[i]<<" "<<sinTwo[i]
<<" "<<termsTwo[i]<<" "<<errorTwo[i]<<endl;
}
system("pause");
file.close();
return(0);
}
The file I am trying to read in has 4 numbers on the first row and 8 numbers on each row after that(it is a table of values comparing sin(x) to a taylor series expansion).
line 13 does normally not compile. Anyway 'termsOne' (and following) is an array of size 0 and does not expand even if you change 'rows'. Use dynamic arrays like std::vector instead