Passing variables between multiple functions

Hello.

I have just started with C++ and as a training excercise wrote this program. It takes some variables, uses them for calculations and then shows the results and saves them to a file.
Here's the problem: If i use this program without calculations.cpp and un-commenting shown lines in main(), everything works perfectly.
However, if i try moving calculations to a seperate file, then the program asks for input 2 times. I reallize it is because i call upon certian functions twice. What i dont get is how to, i dunno, store the variable and call for it from diffrent places. I tried & and *, but that didnt help me (must have misused or misundrestood them). Tried struct and class public:, but somehow that didnt work either; maybe too advanced for me for now.

Can someone point me to a tutorial on how to move variables to multiple functions without said functions running every time ?


header
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
 #include <iostream>
#include <math.h>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <conio.h>
#include <limits>

#define M_PI 3.14159265358979323846

#ifndef LATTICEHEADER_H_INCLUDED
#define LATTICEHEADER_H_INCLUDED

int welcomeMessage();
int millerIndex(char index);
int getData();
long double calcLattDist();
long double defineWaveLength();
long double doubleTheta();
long double lattSpacing();
long double lattConstant();


#endif // LATTICEHEADER_H_INCLUDED 


welocome message
1
2
3
4
5
6
7
8
9
10
#include "latticeHeader.h"

int welcomeMessage()
{
    std::cout << "Welcome to lattice parameters  calculator." << std::endl;
    std::cout <<"(constant a and spacing d_hkl)" << std::endl;
    std::cout << "Currently only cubic cells are supported." << std::endl;
    return 0;
}


data input
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
60
61
62
63
64
65
#include "latticeHeader.h"

int millerIndex(char name)
{
    int index;
    std::cout << "Input " << name << " index" << std::endl; //input of Miller index h
    std::cin >> index;
    return index;
}

long double defineWaveLength()
{
    std::cout << "\n";
    std::cout << "Predefined sources\n";
    std::cout << "\n1 - CuKa - 1.5406A\n2 - CrKa - 2.29A\n3 - FeKa - 1.94A";
    std::cout << "\n4 - CoKa - 1.79A\n5 - MoKa - 0.71A\n6 - AgKa - 0.56A\n0 - other\n";
    std::cout << "Choose source: ";
    int wchoice;
    std::cin >> wchoice;
    std::cout << "\n";

    long double wl;

    switch( wchoice )
    {
    case 1: wl = 1.5406; break;
    case 2: wl = 2.29; break;
    case 3: wl = 1.94; break;
    case 4: wl = 1.79; break;
    case 5: wl = 0.71; break;
    case 6: wl = 0.56; break;
    case 0:
        {
            std::cout << "Input wavelength" << std::endl;
            std::cin >> wl;
        }
    }

    return wl;
}

long double doubleTheta()
{
    long double t;
    std::cout << "Please input double theta in\n1 - Degrees\n2 - Radians" << std::endl;
    int tchoice;
    std::cin >> tchoice;

    switch (tchoice)
    {
        case 1:
            {
                std::cout << "Enter double theta in degrees" << std::endl;
                std::cin >> t;
                return ((t/2)*(M_PI/180));
            }
        case 2:
            {
                std::cout << "Enter double theta in radians" << std::endl;
                std::cin >> t;
                return t;
            }
    }
    return 0;
}


calculations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "latticeHeader.h"

long double lattSpacing()
{
    long double wl = defineWaveLength();
    long double t = doubleTheta();
    long double d;
    d =(wl/(2*sin(t)));
    return d;
}

long double lattConstant()
{
//    int h = millerIndex('h');
//    int k = millerIndex('k');
//    int l = millerIndex('l');
    long double d = lattSpacing();
    long double a;
    a = sqrt(( pow( h, 2 ) + pow( k, 2 ) + pow( l, 2 ) ) *( pow( d, 2 ) ) );
    return a;
}


showing on screen and saving to file
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
#include "latticeHeader.h"

int main()
{
    std::cout << welcomeMessage() << std::endl;
    int h = millerIndex('h'); //hkl was i 3 diffrent functions, someone showed me how to do it once with 3 'name' parameters
    int k = millerIndex('k');
    int l = millerIndex('l');
//    long double wl = defineWaveLength();
//    long double t = doubleTheta();
    long double d = lattSpacing();
    long double a = lattConstant();
//    long double d; //instead of the above
//    long double a; //instead of the above
//    d =(wl/(2*sin(t))); //equation from calculations.cpp
//    a = sqrt(( pow( h, 2 ) + pow( k, 2 ) + pow( l, 2 ) ) *( pow( d, 2 ) ) ); //equation from calculations.cpp
    std::cout << "(" << h << k << l << ")" << "\t" << a << "\t" << d << std::endl; //shows result
    std::ofstream o( "Lattice Parameters.txt", std::ios::app ); //create txt file with name and append
    o << "hkl(" << h << k << l << ")\t a=" << a <<"A\t d_hkl " << d << "A" << std::endl; //saves results to a file
    std::cout << std::endl << std::endl;
    std::cout<<"Calculate another ? (y/n)";
    char rerun;
    std::cin>>rerun;
	if (rerun == 'y' || rerun == 'Y')
    {
        return main();
    }
    else
    {
        std::cout << "Press any key to exit" << std::endl;
    }
}


[edit]
Missed a comment.
[/edit]
Last edited on
What is the "certain function" that is called twice instead of once?
MillerIndex()
defineWaveLength()
doubleTheta()
1
2
3
4
5
6
long double lattConstant(int h, int k, int l, long double d)
{
    long double a;
    a = sqrt(( pow( h, 2 ) + pow( k, 2 ) + pow( l, 2 ) ) *( pow( d, 2 ) ) );
    return a;
}


In main() :
double a = lattConstant(h, k, l, d);
Does that help you? :)
Fan-freakin'-tastic. I does work now.
How ? How does it know that h in lattConstant() is the same h in millerIndex(char name) ?
Nice it worked :)

> How ? How does it know that h in lattConstant() is the same h in millerIndex(char name) ?
Sorry, but can you please clarify your question a bit :)
Last edited on
In lattConstant(int h, int k, int l, long double d) the values h,k,l are taken from millerIndex()

BUT
1
2
3
4
5
6
long double lattConstant(int h, int k, int l, long double d)
{
    long double a;
    a = sqrt(( pow( h, 2 ) + pow( k, 2 ) + pow( l, 2 ) ) *( pow( d, 2 ) ) );
    return a;
}

has no mention of millerIndex(). How does it know where h,k,l comes from?
Last edited on
http://www.cplusplus.com/doc/tutorial/functions/

You already calculated l,h,k,d in your main() function. Then in function main() you call lattConstant(h, k, l, d); which means you pass the values of h, k, l, d respectively in the function main() to the corresponding variables (parameters) h, k, l, d in lattConstant(). And when the function lattConstant() executes the function uses these values you have passed to calculate and return the result for you.

Does it help?
Yes. I made a mistake by forgetting that millerIndex() is called in main() and then lattConstant() is called also from main(), so main() has all the parameters.
So the problem is solved now?

Good day :)
Topic archived. No new replies allowed.