# User defined function problem

This is a discussion on User defined function problem within the C Programming forums, part of the General Programming Boards category; i just finished writing a function to solve the factoral of a user inputted int. In my main loop i ...

1. ## User defined function problem

i just finished writing a function to solve the factoral of a user inputted int. In my main loop i need to display the function and let the user enter as many numbers as they want. After the user enters the numbers they want, they enter a key to quit the program and the factoral of all of the inputted numbers are displayed.

I cannot think right now and don't know what to do. Any tips are appreciated. My code i have so far is below:

Code:
```#include <stdio.h>
#include <math.h>
#define PI 3.141593
#define ot (float)1/3

void factoral (long n)
{

double power;
double dividen;
double sqrt_div;
double num;
double denom;

printf("\n\nPlease enter the number you want to know the factoral of: ");
scanf("%ld", &n);

power = pow(n , n);
dividen = (2 * n + ot) * PI;
sqrt_div = sqrt(dividen);
num = power * sqrt_div;
denom = exp(n);

printf("\nThe factoral of %ld is approximately: %ld\n\n", n, answer);

return;
}

#include <stdio.h>

void main(void)
{

double num;

factoral(num);

return;
}```

2. Hmm. Seems to work OK for me. That is, I get the right answers for n=2,3,4,5. What problem are you getting?

Don't use void main. Use int main, and return zero to signal that the program has finished correctly.

3. What's the point of passing the variable n into your function if the first thing you do is trample all over it with user input?

Moving *everything* into a function is not the way to go... In your case I would get my user input in Main() then pass the user's value into the function. Not only does this make more efficient use of your code, it allows for expansion with menus, other functions etc.

4. i know the main is pointless so far, but in the main i need to make it so the user can enter as many values into the function that they want, and once they enter a key i select like "Q" to quit it prints all the factorals of the numbers they choose.

The function works, i just don't know how to let the user enter as many values as they want and once they quit it displays the factorals

5. what u meant by this program? is it the factors of the number?

6. Originally Posted by 337higgin
i know the main is pointless so far, but in the main i need to make it so the user can enter as many values into the function that they want, and once they enter a key i select like "Q" to quit it prints all the factorals of the numbers they choose.

The function works, i just don't know how to let the user enter as many values as they want and once they quit it displays the factorals
As I said... the first step is to get the user input out of the function itself. Move it into main and get it calling the function for you after the user makes an entry. Get it displaying the correct answers. After that you can put the input function in main in a loop and you should be off to the races...

A properly written function does only one thing. The idea is to hand it a value and get back a result...

Think in smaller blobs!

7. I could not figure out how to loop this without using a goto which my teacher does not like. The program works like i want but i really wish i didn't use a goto. Also, im using longs and doubles but my program gives me a large negative number when you enter any value 13 and above.

Here is my code. Do you see any errors? Thanks in advance:

Code:
```#include <stdio.h>
#include <math.h>
#define PI 3.141593
#define ot (float)1/3

void factoral (long n)
{

double power;
double dividen;
double sqrt_div;
double num;
double denom;

power = pow(n , n);
dividen = (2 * n + ot) * PI;
sqrt_div = sqrt(dividen);
num = power * sqrt_div;
denom = exp(n);

printf("\nThe factorial of %ld is approximately: %ld", n, answer);

return;
}

#include <stdio.h>

void main(void)
{
long num;
x:
printf("\nPlease enter the number you want to know the factorial of. ");
printf("\nEnter 'Q' if you wish to quit: ");
scanf("%ld", &num);

if(getchar() != 'Q')
{
if(num > 0)
{
while(num)
{
factoral(num);
goto x;
}
}
else
printf("You cannot find the factorial of a negative number. Try again.\n");
}

printf("\n");
return;
}```

8. goto is to be avoided and is helpful only in very rare cases... and certainly not from inside a nest of if() statements...

Code:
```#include <stdio.h>

int main(void)
{
long num;
do
{
printf("\nPlease enter the number you want to know the factorial of. ");
printf("\nEnter '-1' if you wish to quit: ");
if (scanf("%ld", &num) == 1 && num > 0 )
factoral(num);
}
while(num > 0);
return 0;
}```

9. Thanks. I never understand when i should use certain loops like while, for, or do. Thanks for the help

10. Originally Posted by 337higgin
Thanks. I never understand when i should use certain loops like while, for, or do. Thanks for the help
Grab some tutorials and read up on what the various loops do... This is core programming skill, you can't let this one slide...

while() loops execute while their entry condition is true ... Use them when coming into a loop with a possibly unknown state...
Code:
```int i = ???
while ( i < 11 )
{  // whatever
}```
Enter the loop and continue as long as i is less than 11... if i is > 10 on entry the loop never executes.

do - while() loops execute at least once, then exit when the condition is untrue...
Code:
```int i = ???

do
{ // whatever
}
while ( i < 11);```
This loop is guaranteed to execute at least once and will continue until i is not < 11 ...

for() loops start at the stated value, run until the condition is false, managing the loop value as they go...
Code:
```for (int i = 2367; i > 0; i -= 2)
{ //whatever
}```
This loop starts at 2367 and will run until i is less than 1, subtracting 2 from i on each repetition.
(For loops are very flexible... read up on these very carefully... )

duzathelp?