I am trying to read data into a file, but I get very unexpected results. I need to play around with the data and do a polynomial interpolation later, so I include a bunch of extra libraries. My code:
//using the standard namespace
using namespace std;
int main()
{
//variable definitions
ofstream outClientFile;
ifstream inClientFile;
string fileName;
string inputname;
string stuff;
string stuff1;
int kpt=0;
//prompt input for finding file name
cout << "Enter the file name to be interpolated, without file extension, extensions are handled internally : ";
cin >>inputname;
cout <<endl;
stuff=string(inputname +".energy");
inClientFile.open(stuff.c_str());
//check if the file opened
if(inClientFile.is_open())
{
cout <<"input file opened sucessfully"<<endl;
cout <<endl;
}
else
{
cout <<"Error opening input file"<<endl;
cout <<endl;
finaloutput();
return 0;
}
cout << "Enter the file name to be outputted, just the name without the extension, file extensions are handled internally: ";
cin >> fileName;
cout <<endl;
stuff1 =string(fileName + ".energy");
outClientFile.open(stuff1.c_str());
//error check if files opended sucessfully
if(outClientFile.is_open())
{
cout<<"output file opened sucessfully"<<endl;
cout<<endl;
}
else
{
cout<<"output file could not be created"<<endl;
finaloutput();
return 0;
}
//varible definitions for data input
int kptcount=0;
int ecount =0;
int linecount=0;
string stuff2;
int line=0;
int totalline=0;
//reading the file here, the problem is probably after this line
getline(inClientFile,stuff2);
inClientFile>>kptcount;
cout<<"Reading "<<kptcount<<" k-points from the input file and a total of ";
getline(inClientFile,stuff2);
I am a little confused, I probably wrote the logic conditions for reading the lines incorrectly, but I do not see the solution to the problem. Any help would be greatly appreciated
Chris
Found the error, there is a memory spacing fault with the g++ compiler working on my laptop. the new routine that works is:
double x1read[kptcount];
double x2read[kptcount];
double x3read[kptcount];
double memoryspacer[kptcount]; //fault with g++ compiler requiring memory spacing
//on my computer
inClientFile>>x1read[1]>>x2read[1]>>x3read[1]>>line;
getline(inClientFile,stuff2);
linecount=line*kptcount+kptcount;
totalline=line*kptcount+kptcount+2;
cout<<linecount+2<<" lines of data"<<endl<<endl;
double energydata[kptcount][line];
Please post a small complete sample of your input file.
And note this program doesn't compile with my compiler:
main.cpp||In function ‘int main()’:|
main.cpp|41|error: ‘finaloutput’ was not declared in this scope|
main.cpp|59|error: ‘finaloutput’ was not declared in this scope|
main.cpp|78|error: ISO C++ forbids variable length array ‘x1read’ [-Wvla]|
main.cpp|79|error: ISO C++ forbids variable length array ‘x2read’ [-Wvla]|
main.cpp|80|error: ISO C++ forbids variable length array ‘x3read’ [-Wvla]|
main.cpp|93|error: ISO C++ forbids variable length array ‘energydata’ [-Wvla]|
main.cpp|93|error: ISO C++ forbids variable length array ‘energydata’ [-Wvla]|
main.cpp|22|warning: unused variable ‘kpt’ [-Wunused-variable]|
main.cpp|65|warning: unused variable ‘ecount’ [-Wunused-variable]|
||=== Build finished: 7 errors, 2 warnings (0 minutes, 1 seconds) ===|
Hi, here is the program again edited to remove the errors you found, it compiled without errors with g++ on my windows 8 laptop, is that strange it would not work on your computer?
I think I have also figured out the trickiest part my program as well, dealing with degenerate crossing points in the interpolation. Is there a faster or more efficient way to do what I want?
double energytemp1=0;
double energytemp2=0;
double energytemp3=0;
double deriv2temp1=0;
double deriv2temp2=0;
double deriv2temp3=0;
double deriv2temp4=0;
double deriv2temp5=0;
double deruv2temp6=0;
double highderiv=0;
int storedindex=0;
int crossingcount1=0;
int crossingcount2=0;
int crossingcount3=0;
int crossingcount4=0;
int crossingcount5=0;
int corssingcount6=0;
for(int z=1;z<(kptcount-2);z++)
{
for(int a=1;a<line+1;a++)
{
energytemp1=energydata[z+2][a]+(energydataderiv1[z+1][a]+energydataderiv1[z][a])/2;
for(int b=a+1;b<(line+1);b++)
{
energytemp2=energydata[z+2][b]+(energydataderiv1[z+1][b]+energydataderiv1[z][b])/2;
//condition 1
if(energytemp1>energytemp2)
{
cout<<"Crossing condition 1 met between k points "<<z+2<<" and "<<z+3<<" for lines "<<a<<" and "<<b<<endl;
crossingcount1++;
deriv2temp1=abs(energydataderiv2[z+1][a]);
deriv2temp2=abs(((energydataderiv2[z][a]+energydataderiv2[z-1][a])/2)*5);
//condition 2
if(deriv2temp1>deriv2temp2)
{
cout<<"Crossing condition 2 met betweem k points "<<z+2<<" and "<<z+3<<" for lines "<<a<<" and "<<b<<endl;
crossingcount2++;
//checking to see if there is a change in the second derivative at that point
//checking to see if there are any other crossings at the same point
for(int c=b+1;c<(line+1);c++)
{
energytemp3=energydata[z+2][c]+(energydataderiv1[z+1][c]+energydataderiv1[z][c])/2;
//condition 4
if(energytemp1>energytemp3)
{
crossingcount4++;
cout<<"Crossing condition 3 met between k points "<<z+2<<" and "<<z+3<<"for lines "<<a<<" "<<b<<" "<<c<<endl;
//checking for higher order crossings
for(int d=c+1;d<(line+1);d++)
{
energytemp4=energydata[z+2][d]+(energydataderiv1[z+1][d]+energydataderiv1[z][d])/2;
if(energytemp1>energytemp4)
{
crossingcount5++;
//sort out the degeracy here.....etc
}
}
}
}
}
}
}
}