Right. So let's go through part of it.
Line 22: if the file isn't open print error message and break out of loop.
Line 28: If you get here, the file must be open.
Line 29: Uh, we alrad know the file is open, so the condition is always true.
Line 44: ... and since the condition at line 29 must be true, this line will never execute.
BTW, at line 35, it's faster to square a number by multiplying it times itself.
Yeah, figured my conditions where all over the place.
This takes care of everything wrong i noticed. Now only the rest after the loop doesn't execute. Not sure why wouldn't while (!std::cin.fail()); //eof doesn't work either not finish the loop on end of file.
As for squaring speed isn't essential here and later on i might be using exponents like 7/9 ;)
EDIT. I got teh stupid. Incorrect order on lines 48-51. Corrected.
#include <iostream>
#include <fstream>
#include <math.h>
#define M_PI 3.14159265358979323846
int main() //everything in this will be elsewhere, true main() has only function calls
{
struct Vars
{
double wl = 0, t = 0;
int h = 0, k = 0, l = 0;
};
std::ifstream file ("params.txt"); //make user definable
Vars vbl;
int lineNum = 0;
double tr;
double d;
double a;
do
{
if (!file.is_open())
{
std::cerr << "Unable to locate file. File params.txt must be in the same folder as Crystal.exe"; //Crystal.exe is name of this program.
break;
}
else
lineNum++;
{
if(file >> vbl.wl >> vbl.t >> vbl.h >> vbl.k >> vbl.l)
{
tr=((vbl.t/2)*(M_PI/180)); //...so below
d=(vbl.wl/(2*sin(tr))); //will be moved to a separate file for calculating functions; use dhayden example
a=sqrt((pow(vbl.h,2) + pow(vbl.k,2) + pow(vbl.l,2))*(pow(d,2))); //As above...
std::cout << "Line " << lineNum << " input: Wavelenght = " << vbl.wl << "[nm] - Double theta = " << vbl.t << "[2deg] " << "(hkl)_(" <<vbl.h << vbl.k << vbl.l << ")\n"; //standarize t units
std::cout << "Line " << lineNum << " results: d=" << d << "nm a=" << a <<"nm \n";
std::ofstream o( "Lattice Parameters Auto.txt", std::ios::app );
o << "Line " << lineNum << "from DATE " << "d_" << vbl.h << vbl.k << vbl.l << "=" << d << "[nm]\ta=" << a << "[nm]\t" << std::endl; //need to add date
}
else
{
std::cerr << "Can't parse line " << lineNum;
break;
}
}
}
while (!std::cin.eof());
file.close();
std::cout << "\nFinished. Press any key to exit.\n";
std::cin.sync();
std::cin.get();
return 0;
}
Managed to incorporate everything into the main program. Thank you very much for your help.
I do have a question, though: In some places you used ¶ms to reffer to the struct, in others as vbl. Why is that ?
double testCalc(const Vars ¶ms) defines a function whose parameter is a const reference to a Vars structure.
double testCalc(Vars params) defines a function whose parameter is a copy of a Vars structure.
Unfortunates this is one of the oddities of C++: sometimes & means "address of", sometimes it means "reference to" and sometimes if means "bitwise 'and' operation"