User defined functions

Pages: 12
I'm writing a code to use the limit definition of a derivative to find a derivative at a point x. I'm using user defined functions to set each part of the code up and i've seemed to run into a problem that i don't quite understand.
when i try to calculate some values in a defined function it outputs a 0 instead of the actual value of the variable.

//David Wood October 18,2018
//Programming assignment CH6
// Finding the derivative of a funtion using The limit defention using user defined functions


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// Declared variables
double C1;
double C2;
double C3;
double DeltaX = .1;
double F_of_x;
double TestTolerance = .001;
double XValue;
double FxDeltax;
double FPrime;
int Number_of_loops = 0;



void Introduction() { // Defined function for the introduction of the code
cout << " This program will calculate the derivative of a function at a point x with some help from the user" << endl;
cout << " The Quadratic function of c1x^2+c2x+c3 is the function that will be used" << endl;
cout << " You the user will give numerical imputs for c1,c2, and c3 and x to determine the derivative at x" << endl;
cout << " " << endl;

}

double Input(double C1, double C2, double C3) { // Defined Function for the user inputs
cout << "Please input your values for C1, C2 and C3: " << endl;
cin >> C1;
cin >> C2;
cin >> C3;
cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << endl;
cout << " " << endl;
cout << " Using this function the program will calculate the derivative at a x value that you input" << endl;
cout << " Please input the x value you wish to use to find the derivative : " << endl;
cin >> XValue;
return C1, C2, C3;

}


int F_X( double F_of_x , double FxDeltax , double FPrime ) {
F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)
cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;

//Defined functions for while loop
FxDeltax = (XValue + DeltaX);// f(x+deltax)
FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime

return F_of_x, FxDeltax, FPrime;
}










int main() {
Introduction();
Input(C1, C2, C3);
F_X(F_of_x , FxDeltax , FPrime );



system("pause");
return 0;
}


return C1, C2, C3;
This looks wrong. This does NOT return three values. It returns one value.

return F_of_x, FxDeltax, FPrime;
Much the same.

You can return ONE object from a function.
okay, so what should i change to make it correct, thats the whole thing im confused on. The Cout for the value of f(x) is 0 for every number i input. what is the problem in that case
Hello Drac0,

Given the function:
1
2
3
4
5
6
7
8
9
10
11
int F_X(double F_of_x, double FxDeltax, double FPrime)
{
	F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)
	cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;

	//Defined functions for while loop
	FxDeltax = (XValue + DeltaX);// f(x+deltax)
	FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime

	return F_of_x, FxDeltax, FPrime;
}

Why are you passing global to a function? Next the function returns an "int", but everything in the function is a "double". You will loose some information from the "double" trying to be stuffed into an "int". Secondly why return anything? The three variables that you are trying to return are global variables, so if their values have changed in the function the global variables were changed before the return statement.

Global variables may seem easy to use, but anything in the program can change their value and it can be hard to track down a problem. All those global variables are better defined in "main" and passed to the functions that need them either by value or by reference.

I have not tested the program yet, but it did compile with out errors.

To expand on what Repeater said you should read up on the comma operator.
http://www.cplusplus.com/doc/tutorial/operators/#comma

Hope that helps,

Andy
I did change the int to double, i only had it like that to try to see if that was the problem. I also changed the return to only return the F_of_x variable, but i don't think that helps my case. The point of this code was to use user functions for every part of the code. So i have to evaluate f_of_x using a user defined function and not evaluate it in the main function. I'm ridiculously confused on why the code spits out a 0 instead of actually calculating the value of f(x). Repeaters reply confused me and i don't really know what he is getting at.

1
2
F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)
cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;


In this section of code, C1 and C2 and C3 are probably zero. That's why F_of_x is zero.
Even if i input c1 ,c2 and c3 as lets say 3 f(x) is 0 still. What would make them zero. is there anything incorrect about that defined function.

double F_X(double F_of_x, double FxDeltax , double FPrime) {
F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)


//Defined functions for while loop
FxDeltax = (XValue + DeltaX);// f(x+deltax)
FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX); // Value for fprime

cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;
return F_of_x;
}

This is what i have now but when ran it still outputs 0 even if i input numbers that shouldn't be zero
we may need to see your whole program with all your fixes, but look at this:

-- you appear to use a global C1, C2, and C3, because either you don't know yet, or you choose to ignore the warnings about not using globals.

--then you made get input function that has local versions of C1, C2, and C3, highlighting the problems with globals. You think its modifying the globals, but its not.

--then you try to compute the value, with the global versions, which are probably being set to zero by your compiler (some do this for you in debug mode, or whatever luck of the draw maybe).

solution: get rid of globals and be sure that C1, C2, and C3 are passed by reference into get input function.

pass by reference just means add a & in front:
void foo (int &x); //pass x by reference

as a side note, local variables are more useful than passing things in this way (the dx function). just have f-of-x be a local variable. Its not hurting anything, but the gist of what I am seeing here is a lack of understanding of how to use parameters.

Last edited on
Hello Drac0,

As I said earlier you are sending global variables to the function. So in a function definition like double Input(double C1, double C2, double C3). The parameters become local variables to the function over shadowing the global variables, so the global variables never receive the new values.

Once I removed the parameters form the function definitions and function calls the program worked. Now all the variables in the functions are using the global variables not the local variables.

Repeaters reply confused me and i don't really know what he is getting at.


You will need to be more specific about what part of Repeaters's reply confuses you.

For the most part the program works. You ar just going about some things the wrong way.

Hope that helps,

Andy
So i do understand the difference between a global and local variable but im still confused on what you did to make this work. What do i need to change. Also my assignment required me to used user defined functions for different parts of my code. 1 for the introduction, 2 for the user inputs, 3 evaluating the function ,and 4 calculation the derivative ( which i haven't gotten to yet because of this problem.)

My problem is how do i get these values to roll over to other user defined functions while keeping the 4 parts of my code into 4 different user defined functions.
@draco,
Values are not "rolled over" to other user-defined functions: they are either passed as arguments, returned as the return value of a function, or they are global variables: make your choice, but don't do multiple things for the same variable.

In order that the derivative function be usable for lots of different functions, any parameters needed to evaluate the particular function (here c1, c2, c3) can't realistically be passed as arguments because they are too specific to your quadratic function. These are the only variables that I can see a cogent argument for making global (otherwise they would have to be tied into f as a function object). All other variables should, I think, be passed through the argument list or returned as function results.

Please upload below this the latest version of your code. And PLEASE, PLEASE, PLEASE PUT IT IN CODE TAGS (first item in the format menu to the right).
My code hasn't really changed much from the top. I've just been working in another project trying to get everything figured out. Would it be possible for me to pass the cin values by reference to get them to be access in the other parts of the defined functions or is that not going to work either
I used pointers in my input part of my code and it seemed to work out.
Im just assuming that ill have to do that for the rest of my calculations. Ill put the work in progress of the test code i have below.

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
#include <iostream>
#include <cmath>
using namespace std;

double C1, C2, C3;
double XValue;
double DeltaX = .1;
double F_of_x;
double TestTolerance = .001;
double FxDeltax;
double FPrime;
int Number_of_loops = 0;






double Input(double *C1, double *C2, double *C3, double *XValue) {
	cout << "enter c1 c2 c3" << endl;
	cin >> *C1 >> *C2 >> *C3;
	cout << "enter a x value" << endl;
	cin >> *XValue;
	
	return *XValue;


}

double F_x(double F_of_x, double FxDeltax, double FPrime) {
	F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3;
	FxDeltax = (XValue + DeltaX); // f(x+deltax)
	FPrime = ((C1*pow(FxDeltax, 2) + C2 * (FxDeltax)+C3) + (F_of_x)) / (DeltaX);
	cout << " The Value for F(x) using the inputed information is : " << F_of_x << endl;
	return F_of_x;
}





int main() {
	int a;

	Input(&C1, &C2, &C3, &XValue);
	
	F_x(F_of_x, FxDeltax, FPrime);




	system("pause");
	return 0;
}
Last edited on
Final code looks like this after figuring out where i went wrong
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//David Wood October 18,2018
//Programming assignment CH6
// Finding derivative of a polynomial using the magic of user defined functions


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// Declared variables 
double C1; // user input 1	
double C2; // user input 2 
double C3; // user input 3
double DeltaX = .1; // value of deltax
double F_of_x; // defined variable for f(x)
double TestTolerance = .001; // starting value for the tolerance of the function to calculate derivative in a loop
double XValue; // defined variable for point at which derivative will be taken
double FxDeltax; // defined function for F(x) + DeltaX
double FPrime; // defined function for the first derivative of funtion 
int Number_of_loops = 0; // counter for the number of iterations it took loop to calculate the code



void Introduction() { // Defined function for the introduction of the code
	cout << " This program will calculate the derivative of a function at a point x with some help from the user" << endl;
	cout << " The Quadratic function of c1x^2+c2x+c3 is the function that will be used" << endl;
	cout << " You the user will give numerical imputs for c1,c2, and c3 and x to determine the derivative at x" << endl;
	cout << " " << endl;

}

double Input(double *C1, double *C2, double *C3, double *XValue) { // Defined Function for the user inputs ( Set as points to pass reference
	cout << "Please input your values for C1, C2 and C3: " << endl; // prompting user to input values
	cin >> *C1; // user inputs for c1 c2 c3
	cin >> *C2;
	cin >> *C3;
	cout << " The function you entered is : " << *C1 << "x^2 + " << *C2 << "x + " << *C3 << endl; // Shows user the function that they entered
	cout << " " << endl;
	cout << " Using this function the program will calculate the derivative at a x value that you input" << endl;
	cout << " Please input the x value you wish to use to find the derivative : " << endl;
	cin >> *XValue; // user input for x value of function
	cout << " The x value the derivative will be calculated at is " << *XValue << endl;

	return *XValue;

}

// Defined user function for Variables of while loop
double F_X(double *F_of_x, double *FxDeltax, double *FPrime) { // values set as points to pass reference to other defined functions
	*F_of_x = (C1 * pow(XValue, 2)) + (C2 * XValue) + C3; // value for f(x)


	//Defined functions for while loop
	*FxDeltax = (XValue + DeltaX);// f(x+deltax)
	*FPrime = ((C1*pow(*FxDeltax, 2) + C2 * (*FxDeltax)+C3) + (*F_of_x)) / (DeltaX); // Value for fprime

	cout << " The Value for F(x) using the inputed information is : " << *F_of_x << endl;
	return *F_of_x;
}

void Calculation() {
	while (abs(FPrime - TestTolerance) > .001) {
		DeltaX = DeltaX / 2;

		TestTolerance = FPrime;


		FxDeltax = (XValue + DeltaX);

		FPrime = ((C1 * (pow(FxDeltax, 2)) + (C2 * (FxDeltax)) + C3) - (F_of_x)) / (DeltaX);

		Number_of_loops++;


	}
	
	cout << " The derivative of F(x) at x = " << XValue << " is : " << setprecision(3) << FPrime << endl;
	cout << "This code took " << Number_of_loops << " iterations to find the accuracy of the derivative " << endl;


}






int main() {
	Introduction();
	Input(&C1, &C2, &C3, &XValue);
	F_X(&F_of_x, &FxDeltax, &FPrime);
	Calculation();


	system("pause");
	return 0;
}

No @draco, you are making no attempt to understand what any of us has written.

It is nonsense to pass C1, C2, C3 as pointers - I have no idea what thought process suggested that to you. These three are about the only variables I can see with a decent reason to keep as global variables, NOT also needing passing by arguments. With the exception of a possible function pointer (see below) there is no other sensible place to use pointers in this problem.

As I put in my previous post, DON'T declare a variable as global AND as a procedure's argument. For example, FxDeltax: you declare that as global and then try to declare it again as an argument.

I have no idea whether your function F_X is intended to return the function value or its derivative! It is an unholy mixture of the two. Why is it calculating a variable called FPrime which (i) isn't used and (ii) doesn't represent "f prime' - i.e. the derivative of f.

I would suggest that you had one function
F(x)
which returns the function value at x, and a second function
Deriv(f,x)
which takes a pointer to an (arbitrary) function f, and returns the derivative at x.

Alternatively, if you don't want function pointers then just have
F(x)
and
Deriv(x)
which just calls the function F at x+dx and x (or, possibly, x-dx).

Either way, you should (i) go to a maths book and understand how a derivative is defined first; (ii) sort out your bizarre and confusing naming which is leading you to confuse a function with its derivative; (iii) read up on how variables are passed as function arguments in c++ (or, indeed, many other programming languages).
Last edited on
okay buddy first off, i dont need to go to a" maths book" to understand how a derivative is defined, im not a fucking dipshit. This is a limit definition of a derivative, the while loop calculates the limit definition using deltax and the loop makes delta x approach 0 like how the derivative in the definition works. the tolerance finds the accuracy of the loop and when they match thats the given derivative. As for the code maybe i did mess some stuff up and ill fix it. it is pretty obvious i don't understand what you guys are talking about so why would you keep explaining it the same way over and over and over. There is no point in trying to help by basically just telling me my shit is wrong. This compiled and ran after i tried fixing it myself by being totally confused by this whole thing. as for my naming system, it might not be the best but then again i can fucking fix it and i will fix it. its not confusing to me because im the one who wrote the program and i have a feeling that you've felt the same way a few times in your life of coding. your last reply is also the first time in this thing that someone actually suggested me doing something other than just telling me shit is wrong. If i dont understand the shit try to help me understand instead of just repeating yourself using some slight variations in your paragraphs. I thought i made the problem i had pretty clear but the "help" i got was not clear at all
Last edited on
@Drac0

If you don't understand something, then say so. In my mind that is better than:

If i dont understand the shit try to help me understand instead of just repeating yourself using some slight variations in your paragraphs. I thought i made the problem i had pretty clear but the "help" i got was not clear at all



Realize the other respondents are trying to help, and they do know what they are talking about. They did actually explain what they were saying.

I understand coding can be frustrating for those new to it, but the sooner one follows the main concepts, the easier it will be. By main concepts, I mean not using globals, use meaningful names for variables, write code in a clear easy to follow fashion, just to name a few. Good code should tell a story of what is happening, so it should be easy to follow even for someone who isn't familiar that particular topic. When someone who has a lot experience with a topic mentions they find it hard to follow, then that is something to take on board.

I also understand how tough University can be: time is a finite resource, there is a lot of info to absorb, one just wants to get their assignments out the door. When things don't go well, it's easy to get frustrated.

So take a deep breath, try to follow the advice given.
I understand Drac0's rant, to be honest. He did end up getting a working product in the end, despite the global variables. I think just reading paragraphs of suggestions, without seeing clear code examples of those suggestions, would be hard to learn from. Understanding correct style and understanding why global variables are bad can be hard for such a short program. It sounds like Drac0 did try to follow the suggestions that people gave, but then reverted back to what he knew worked after his program fell apart. That isn't to diminish other's work, I'm just saying I can see how it's frustrating for him.

What should have happened is that we shouldn't have ever been at the point where he was declaring 20 variables before main in the first place. His proper instructor/book should have started with simpler examples to show how to pass variables by copy or reference as parameters/return values, so that the habit of declaring everything as global didn't happen in the first place.

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
// Example program
#include <iostream>

//
// Pass a variable by value to use it, and return a new value
//
double use(double z)
{
    return z * (z - 1.0) + 3.5;   
}

//
// Passing a variable by reference to modify it
//
void modify(double& z)
{
    z = z * (z - 1.0) + 3.5;   
}

int main()
{
    using std::cout;
    
    double x = 2.0;
    double y = use(x); // passed by value
    
    cout << x << '\n';
    cout << y << '\n';
    
    double z = 3.0;
    modify(z); // passed by reference and modified
    cout << z << '\n';
}
Last edited on
@Drac0,
I am referring to to your final code posting in
http://www.cplusplus.com/forum/beginner/244443/#msg1082853

Global variables:
- I think the only variables with any reason to be global here are C1, C2, C3 - they are specific to your particular function being a quadratic and would make argument lists quite long otherwise. There is a possible argument for TestTolerance, but it is only used in the derivative function and might be better defined there.
- All of your other variables should be passed through the function calls: either as arguments or as return values. They should not be global variables.
- In particular, initialising Number_of_Loops here would mean that if the derivative function were called twice then it would be incremented further each time; for example, it might tell you that you took 10 loops the first time, 20 the second, 30 the third etc.


Introduction:
- OK, but keep it short.


Input:
- If C1, C2, C3 are global variables then don't declare (pointers to) them in the argument list. You can simply set them directly.
- There is no need to use a pointer variable for X. Declare it as a local variable and just
return X;
at the end of the function. Note that it would reduce complexity to call it X, not XValue.
- Do not have global variable XValue; the function is returning this.
- I suggest that the declaration for the Input function should simply be
double Input()
It will return X as the function return value, whilst C1, C2, C3 are global variables anyway.
- If you accept this advice, then this function becomes
1
2
3
4
5
6
7
8
9
10
11
double Input() {
	cout << "Please input your values for C1, C2 and C3: "; 
	cin >>  C1 >> C2 >> C3;     // These (slightly reluctantly) are global variables
	cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << "\n\n";

	double X;
	cout << " Using this function the program will calculate the derivative at a x value that you input\n";
	cout << " Please input the x value you wish to use to find the derivative:\n";
	cin >>  X;
	return X;           // The position is sent back as the return value of the function
}




Double F_X:
- It is difficult to sort this out. The logical thing would be to send it the value of X (passed by value through the argument list) and simply to return the value of the function at this point.
- Thus, it could be as short as
1
2
3
4
double F( double X )
{
   return C1 * X * X + C2 * X + C3;
}

- Note that I suggest simplifying names a bit here.
- In particular, you shouldn't try to do anything at a neighbouring point (X+dX) here.
- Fprime - as calculated in your function in the latest code - doesn't really calculate anything. As written, it is not an approximation for the derivative of F (as its name definitely implies, or as it is used in the following function). It simply shouldn't appear in this function or in a list of global variables.


"Calculation"
- Bear with me, I think this function should simply be called Deriv and have declaration
double Deriv( double X )
- If DeltaX and FPrime are used here they must have been initialised elsewhere. Here, you have used global variables, so any values they take on entry would be whatever they were on the last call - in the case of DeltaX this would be badly wrong if the function were called multiple times, since you would pick up from an already-small value. Similarly, Number_Of_Loops would be further incremented from any earlier call, not started afresh.
- This routine should do nothing but calculate the derivative (and, possibly output Number_Of_Loops, but only as a debugging statement).
- Your estimate of the derivative should call your existing F function; something like
FPrime = ( F(X+DeltaX) - F(X) ) / DeltaX;
although, since F(X) is used repeatedly, you could store this value (as indeed you do).


main()
- This should perform most of the output, based on the results of calls to other functions, not using global variables.
- My last reply was written between you posting two versions of code, so I was completely taken aback by you suddenly switching to pointers. The use of
F_X(&F_of_x, &FxDeltax, &FPrime);
in particular, caused me to wonder why you had suddenly decided to do this. The least frustrating request would be to ask you to consider @Ganado's illustration of simple function calls.


Other comments
- Good to comment code, but try not to overdo it. An appropriately-named variable is often sufficient without further comment.
- There is no need at all for pointers here.
- There is no need for global variables (other than probably C1, C2, C3).


I'm afraid that any code samples I would produce here would end up simply doing your assignment for you. Past experience suggests that an innocent piece of unrelated code to illustrate an approach often elicits a "don't understand what that has to do with my problem" response.

Last edited on
Hello Drac0,

I do apologize for loosing track of this topic over the weekend.

Cut the guys some slack. Sometimes I think they forget that they are dealing with beginners that do not understand everything, but what they say is correct and good advice.

Given this code:
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;  // <--- Best not to use.

//using std::cout;  // <--- Better choice.
//using std::cin;
//using std::endl;

// Declared variables  <--- This may seem easy, but it is best not to use global variables.
double C1;  // <--- Compiler is likely to initialize global variables, but do not count on it.
double C2;
double C3;
double DeltaX = .1;  // <--- Unless this has to change in the program it should be made a constant.
double F_of_x;
double TestTolerance = .001;  // <--- Unless this has to change in the program it should be made a constant.
double XValue;
double FxDeltax;
double FPrime;
int Number_of_loops = 0;  // <--- Unless this has to change in the program it should be made a constant.

double Input(double C1, double C2, double C3)  // Defined Function for the user inputs
{
	cout << "Please input your values for C1, C2 and C3: " << endl;
	cin >> C1;
	cin >> C2;
	cin >> C3;
	cout << " The function you entered is : " << C1 << "x^2 + " << C2 << "x + " << C3 << endl;
	cout << " " << endl;
	cout << " Using this function the program will calculate the derivative at a x value that you input" << endl;
	cout << " Please input the x value you wish to use to find the derivative : " << endl;
	cin >> XValue;
	return C1, C2, C3;
}

int main()
{
	Introduction();

	Input(C1, C2, C3);

	F_X(F_of_x, FxDeltax, FPrime);

	system("pause");

	return 0;
}

Lines 1 - 3 the order makes little difference, but my preference is to order them this way. Because "iomanip" works with "iostream" I like to keep them together.

Line 5 the comment says it all.

Lines 7 - 9 are a better replacement for line 5.

Lines 11 - 21 are global variables. Although most of these variables are better defined in "main" and passed to the function that would need them. As it has been said "C1", "C2" and "C3" have a better use as global variables.

The thing about global variables is that they have file scope. This means that the entire file can see them and has access to them. So whether a function comes before or after "main" it can see these variables.

Read this for more information on scope http://www.cplusplus.com/doc/tutorial/namespaces/

Now we will look at what is happening in the program.

Line 41 in "main" you are calling the function "input" passing three parameters or variables "C1", "C2" and "C3". At this point you are passing the global variables thinking that is what is needed. The problem is at line 23. This double Input(double C1, double C2, double C3) defines "C1", "C2" and "C3" as local variables meaning that they are local to the function and take precedence over, over shadow or block out, the global variables. So inside the function when you refer to "C1", "C2" and "C3" these are the local variables not the global that you want. The changes you make to "C1", "C2" and "C3" stay in the function and the real problem is when the function ends so do the variables.

The use of the comma operator on your return statement will only return "C3" since a return statement in a function can only return one item.

See:http://www.cplusplus.com/doc/tutorial/operators/#comma The rest of the file is good reading too.

As I said earlier to fix the problem this is what I did:

In "main" you call the function this way: Input(); and the function definition is double Input(). This will allow the function to use the global variables not the local variables.

I did the same for the other function and the program ran fine although I can not say if the math is correct it is a bit beyond what I know right now and would take some research to check. For now I will take it as correct.

Not important right now or a problem, but you have defined some global variables that are not used or used yet.

Last point it is best not to use "system" anything. At its worst it leaves the program vulnerable to hackers otherwise it limits its use to Windows systems.

I use this as a replacement to using "system("pause");":
1
2
3
4
5
6
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();

If there is anything you do not understand let me know.

Hope that helps,

Andy
Pages: 12