jlucero77 wrote: |
---|
So can someone please help tell me why it's not going to my other functions please. |
The original code and this question are deeply puzzling to me. The original code doesn't compile, not even after adding the missing lines before the start of
main()
And because it doesn't compile, in my simplistic way of thinking, there is no way of knowing what it does or doesn't do when it is run. So the code posted above cannot be the same code as that for which the question was asked, surely?
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
|
#include <iostream>
using namespace std;
double numsum(double A);
double fact(double B);
int main()
{
double x;
int n;
cout << "Enter the number of how many times you want to calculate the sum of exponentials" << endl;
cin >> n;
x = numsum(x)/ fact(factorial);
cout << "This is the sum of the exponential " << x << endl;
return 1;
}
double numsum(double A)
{
int n;
int i = 1;
double sum = 1;
while(i <= n)
{
sum = sum * i;
i++;
}
return sum;
}
double fact(double B)
{
double factorial = 1;
int j = 1;
while(j <= n)
{
factorial =*j;
j++;
}
return factorial;
}
|
Here are the problems with compiling this code.
At line 14, the variable
factorial
is undeclared.
At line 38,
while(j <= n)
the variable n is undeclared.
That can be remedied by changing that line to
while(j <= B)
At line 40,
factorial =*j;
this line is trying to calculate the value of
*j
and assign it to factorial. It should of course read
factorial *= j;
Having made those changes, the code will now compile, but there are sill warning messages which should be observed and corrected.
At line 21, the function receives a parameter
A
double numsum(double A)
but nowhere in the function is the parameter A actually used. It is never mentioned again. And that is one of the important things about the way functins are used.
At line 10, in function main(), there is this :
|
x = numsum(x)/ fact(factorial);
|
Here, the variable x is passed as an input parameter to the function numsum. The value of x is copied to the variable A and then inside function numsum, that variable should be used.
There is another problem here. In function main(), x has not yet been assigned any value, so it doesn't make sense to pass it as a parameter. As was already mentioned, variable factorial not only hasn't been assigned a value, it doesn't even exist, so that doesn't make sense either.
All of this suggests the basic concepts of how to call a function using parameters is not fully understood, which is why I suggested (and now repeat) my recommendation to read the tutorial page on functions.
http://www.cplusplus.com/doc/tutorial/functions/
All of this is really just the basic concepts, and until it is understood reasonably well, it will be difficult to move on to more advanced topics.