How to declare null values in C

0

My problem is the following I am creating a loop while that can not receive values smaller than or equal to zero, and would like to also declare that it can not receive empty values for the program to force the user to enter a number and not enter with no field in scanf() .

  printf("Digite o valor da posicao X:");
    scanf("%d",&x);
    //faz a verificação se o numero é 0 ou < 0
  while (x <= 0){
    printf("0 ou < 0 nao forma um triangulo por favor digite novamente a medida X\n");
    scanf("%d",&x);
  }

When the user just type enter he accepts without any data on the console, in case he wanted the system to accept only if a number was entered on the console.

    
asked by anonymous 09.10.2018 / 14:29

1 answer

0

I believe this is what you want. You verify that the entry was valid, the scanf() function provides this as a return, just read the documentation . And you can also validate the entry of the data with the value you want. All this in a simple loop.

#include <stdio.h>

int main(void) {
    int x;
    while (scanf("%d", &x) != 0 && x <= 0) printf("0 ou < 0 nao forma um triangulo por favor digite novamente a medida X\n");
    printf("%d", x);
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
09.10.2018 / 14:52