Home > Mobile >  Trying to run a calculation inside an if-else statement and it's stalling - C
Trying to run a calculation inside an if-else statement and it's stalling - C

Time:02-07

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;
}

CodePudding user response:

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;
}

CodePudding user response:

#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.

CodePudding user response:

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.

  •  Tags:  
  • Related