# Thread: C Help. square and cube a user inputed number

1. ## C Help. square and cube a user inputed number

So I have to write a function that returns the square and cube of a number. The function must:
Prompt the user for the number
Print the number, its square, and its cube

The prompting, calculation of square and cube, and printing must all be done by calling separate functions:
num_prompt() prompts and gets the number from the user
sq_cube() calculates the square and cube of the number
print_sq_cube() prints out the square and cube of the number

I don't know what's wrong...this is what i have...I can't get it to find my user inputted number and calculate its square and cube to print it...heres what i have so far...
Code:
```#include<stdio.h>
#include <math.h>
double num_prompt(double base);
double sq_cube(double square, double cube);
double print_sq_cube(double base, double square, double cube);
int main(void)
{
double num_prompt(double base);{
double base;
printf("Enter the base:\n");
scanf("%d", &base);
}
double sq_cube(double square,double cube);{
double base,square,cube;
for(base;base;base++);
square= pow(base,2);
cube= pow(base,3);
}
double print_sq_cube(double base, double square, double cube);{
double base;
printf("Base Squared Cubed\n"
"---- ------- -----\n");
scanf("%d",&base);
printf("%&base,&square,&cube\n");

}
}```

2. I believe my problem is in sq_cube program and then I dunno how to get that to print

3. You have several problems:

1) I don't know what the for(base;base;base++) is about. Delete that nonsense

2) Your function sq_cube is supposed to return a double value but doesn't.

3) You are NOT PASSING base to your sq_cube function. How is it supposed to compute the square and cube if it doesn't know whose square and cube to compute?

4) Also if you want to modify parameters passed to a function such as square and cube you need to pass them by reference not value. (i.e. pass their address)

5) I dont understand why you need a print function just to print 2 double values.

6) Don't make your life more difficult with pow if you are computing such small values. Just use base*base as square and base*base*base as cube. It looks to me that you have other more pressing things to learn about C than how to use pow.

4. It might help you to have TWO separate functions. One that returns the square and one that returns the root such as:

double square(double x){
/* returns the double of x */
}

double cube(double x){
/* returns the cube of x */
}

5. Okay, Thats what i don't get...is how to pass the the base to the sq cube function. I took care of #1 and 2 and 6. and for 5...thats just what my assignment is...has to be 3 seperate functions.

Code:
```#include<stdio.h>
#include <math.h>
double num_prompt(double base);
double sq_cube(double square, double cube);
double print_sq_cube(double base, double square, double cube);
int main(void)
{
double num_prompt(double base);{
double base;
printf("Enter the base:\n");
scanf("%d", &base);
}
double sq_cube(double square,double cube);{
double base,square,cube;
square=base*base;
cube= base*base*base;

}
double print_sq_cube(double base, double square, double cube);{
double base;
printf("Base Squared Cubed\n"
"---- ------- -----\n");
scanf("%d",&base);
printf("%&base,&square,&cube\n");

}
}```

6. Ok. While I would just like to mention for the record that I completely disagree with whomever has given you this assignment in this form, here is how you can fix it. (i will go through what you must do for each of the 3 functions but will not write the entire code for you. I want you to give it a try and i'll help you out with errors and such)

1) for function num_prompt

This function is supposed to read base using scanf which you are doing correctly but it doesn't take base as an argument. Instead the function header should look like this
Code:
```double num_prompt(){
double base;
/* read base like you are currently doing it */
return base; /* this allows this function to pass on the value read in base to other functions */
}```
2) the sq_cube function

Here you need to change the function header so that you are passing the addresses of cube and square instead of their value. If you don't understand why this is so, PLEASE READ THE FAQ or some online C resource on PASSING ARGUMENTS BY VALUE VS BY REFERENCE. This is a crucial part of C. If you don't learn this you will never be able to use the language.

In any case: the function header should look like this:

Code:
```void sq_cube(double base, double *square, double *cube){
/* base is the value of base you are passing to this function to compute square and cube */
/* square and cube are references to variables that should be declared and initialized in main function */
/* in order to compute square and double assign the correct values to (*cube) and (*square) instead of cube and square --- remember those are addresses not doubles */
}```
3) finally the print function would just take two arguments passed by value cube and square and print the results

FINALLY IN MAIN:

- you need to declare three variables
double cube;
double square;
double base;
- initialize them to 0.0 or whatever you wish.
- get the value of base by making the assignment:
base = num_prompt(); --- num prompt returns a double which is read inside that function
- call sq_cube:
sq_cube(base,&square,&cube); --- note i'm passing the ADDRESS of square and cube variables from main, so the function can write the value at that address
- call print function(square,cube) --- as values, since this function doesn't change cube or square.