0

I'm in an introductory code class for C and I'm trying to run this square and cube calculation if a number greater than 1 is entered, but it stalls if I run it. I'm not getting any errors and I've tried different ways to make this work through tips here, but nothing is happening and I don't know enough to get what's wrong.

#include <stdio.h>

int main ()
{
    int num, square, cube;
    
    printf("What is your number? ");
    scanf("%d", num);
    
    if (num > 1)
    {
        square = num * num;
        cube = num * num * num;
        
        printf("\nThe square of your number is ");
        printf("%d", &square);
        printf(" and the cube of your number is ");
        printf("%d", cube);
        printf(".");
    }
    
    printf("\nPlease enter a number greater than 1.");
    
    return 0;
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
Chris
  • 3
  • 2
  • 2
    add & in scanf, remove & while printing & operator prints the memory location of variable – THUNDER 07 Feb 07 '22 at 04:45
  • 2
    `scanf("%d", num);`->`scanf("%d",&num);` (`scanf()` expects a pointer), `printf("%d", &square);`->`printf("%d", square);`. – JASLP doesn't support the IES Feb 07 '22 at 04:45
  • 3
    Also, is the last `printf` supposed to be inside an `else` block? – kaylum Feb 07 '22 at 04:46
  • 1
    Note that `num` will be unintialised if `scanf` fails for any reason. Usually because of non-integer input. Should always check the return value of `scanf`. – kaylum Feb 07 '22 at 04:50
  • 1
    Your compiler should issue a warning in the line `scanf("%d", num);`. Make sure that you have all compiler warnings enabled. [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Feb 07 '22 at 05:08
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? Doing this should at least allow you to determine which line of your program is failing. If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Feb 07 '22 at 05:25

4 Answers4

1

Add & in scanf, remove & while printing. The & operator evaluates to the memory location of the variable. In scanf we are supposed to use &, so that the input is stored in the memory address obtained by writing &var.

Exit the program after printing the square and cube, or place the next printf in an else block.

#include <stdio.h>

int main ()
{
    int num, square, cube;
    
    printf("What is your number? ");
    scanf("%d", &num);
    
    if (num > 1)
    {
        square = num * num;
        cube = num * num * num;
        
        printf("\nThe square of your number is ");
        printf("%d", square);
        printf(" and the cube of your number is ");
        printf("%d", cube);
        printf(".");
        return 0;
    }
    
    printf("\nPlease enter a number greater than 1.");
    
    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
THUNDER 07
  • 521
  • 5
  • 21
0
#include <stdio.h>

int main ()
{
    int num, square, cube;
    
    printf("What is your number? ");
    scanf("%d", &num);
    
    if (num > 1)
    {
        square = num * num;
        cube = num * num * num;
        
        printf("\nThe square of your number is ");
        printf("%d", square);
        printf(" and the cube of your number is ");
        printf("%d", cube);
        printf(".");
    }
    else
        printf("\nPlease enter a number greater than 1.");
    
    return 0;
}

printf("%d", num); you made mistake use printf("%d", num); when you use Address operator & in print statement it will print address not value so

printf("%d", square); // remove & sign

Also put last print statement under else otherwise it will run everytime

enter image description here

Why we should use ampersand (&) in scanf. The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.

M. Twarog
  • 2,418
  • 3
  • 21
  • 39
  • I can't believe it was that simple. I guess that means at least I had most of it correct. Thank you so much! – Chris Feb 07 '22 at 04:56
  • 1
    You may want to consider posting the program output as text, not as an image. See [this link](https://stackoverflow.com/editing-help#syntax-highlighting) on how to disable syntax highlighting in a code block. – Andreas Wenzel Feb 07 '22 at 05:14
  • Sorry I want to write printf which by mistake I wrote scanf. Thanks for highlighting I fix that issue in my answer – M. Twarog Feb 07 '22 at 06:01
  • It was typo mistake I want to explain printf and mistakenly I wrote scanf – M. Twarog Feb 07 '22 at 06:02
  • You wrote `printf("%d", num);` twice in the text of your answer (not the code block). That does not make sense. I believe that you intended to write one with `&` and one without `&`. – Andreas Wenzel Feb 07 '22 at 06:12
  • I believe it would make more sense to mention the problems with `printf` and `scanf` together in your answer, because those two problems are closely related, as both problems are about the `&` address-of operator. Instead, you first mention the problem about `printf`, then change the topic to `else` and present the output of the program, and only then do you mention the problem with `scanf`. In that repect, your answer does not seem well structured in its current state. You may want to consider improving it. – Andreas Wenzel Feb 07 '22 at 06:20
0

You are experiencing a common error called "Segmentation fault" which occurs when you access a memory location which is not allowed. As others have pointed out, use & operator in scanf while reading the num variable.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

you don't have to use & in printf

#include <stdio.h>

int main ()
{
    int num, square, cube;
    
    printf("What is your number? ");
    scanf("%d", num);
    
    if (num > 1)
    {
        square = num * num;
        cube = num * num * num;
        
        printf("\nThe square of your number is ");
        printf("%d", square);
        printf(" and the cube of your number is ");
        printf("%d", cube);
        printf(".");
    }
    
    printf("\nPlease enter a number greater than 1.");
    
    return 0;
}