I'm a beginning C programmer, trying to write a simple little program. The goal is to find the cube of a number (i.e. 2 * 2 * 2=8), by using a self-made function and then passing the argument from the main function to the self-made function then having that number go back to the main function and print the result. Here's what I have:
#include %26lt;stdio.h%26gt;
int test(int first);
int main(void)
{
int number, cube;
printf("Please enter a number: ");
number = scanf("%d", %26amp;number);
cube = test(number);
printf("The cube is %d\n", cube);
return 0;
}
int test(int first)
{
int a, b;
b = first;
a = b * b * b;
return a;
}
I can't figure out what's wrong. Any help would be greatly appreciated!
How do I pass parameters in C?
Here is yours problem:
%26gt;%26gt; number = scanf("%d", %26amp;number);
scanf first scans for the desired formatted string and stores the value in number, but returns the number of extract tokens, 1 in this case, which gets stores also in numbers. After that line, numbers will always be one.
correcct it to:
scanf("%d", %26amp;number);
and it should work also.
Also, you can simply implement test as
int first (int n) {
return n * n * n;
}
Reply:in your program,error part is number=scanf("%d",%26amp;number);
if you use this statement, the output is 1.
Reply:when you compile what does it do, does it give you a correct number or does it output wrong, im not good with C but i do know C++ and that program is pretty much the same as my C++ i know, my yim is in my profile, if you want to yim me i can help you through it
btw, i wouldnt call the main function as void just leave it blank ()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment