Import from file, calculate and repeat

Pages: 12
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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;
}
Last edited on
That looks good.
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 &params to reffer to the struct, in others as vbl. Why is that ?

 
double testCalc(const Vars &params)


vs
 
Vars vbl;


I know it's working, but why ?
Last edited on
double testCalc(const Vars &params) 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"
Topic archived. No new replies allowed.
Pages: 12