how to compare null/null value when type character in C

Asked

Viewed 577 times

1

    printf("\nteste -> "); 
    scanf("%i", &opAjuda);

/* opAhelp is to receive a whole, but I want to do an if here, saying that if a person writes a character instead of the whole one, it goes to the screen helps and shows the message that is inside the if. And then one has to type again, in case, an entire number. */

    if (opAjuda == 0) {

        /*system("clear");
        telaAjuda ();
        printf("\n\tTENTE NOVAMENTE!\n");
        printf("\n\tOBS: Digite um numero relacionado ao topico que deseja.\n");
        subMenu_Ajuda();*/

         //telaPrincipal ();

         printf("\n%s", opAjuda); //teste pra ver o q estava imprimindo

    } else {

       //Aqui fica o meu switch case
    }

When I type a numeric value, the program works fine, but when I type some character (other than the special ones I’ve already defined and I’ve already made the ifs for it) then when I type a character it is in an infinite loop. When I did the test to see what I was printing when I typed a character it prints a NULL (null) value, I tried to represent this value in if but I’m not getting it. Can someone help me?

NOTE: I want to do this because I’m thinking about the user, the program is written and has tips saying it is to type a number, but not all users follow what is written, and I want the program to be prepared if it happens.

2 answers

2

The function scanf returns an integer, being value 1 when the reading is no error. Common example.

int res=0, in=0;

res = scanf("%d",&in);
if(res==1){
  //seu codigo
}
else{
  //tratar o erro
}

to your problem the solution would be to create a loop being the exit condition the return of scanf function.

Example:

while(scanf("%d",&in)!=1){
   prinf("valor inválido, digite um numero inteiro\n");
}

Obs.: how you put the linux tag, I believe this is yours operating system. Soon when you have any questions regarding any function in C you can take a look at the documantation, using the program man which is already installed in all linux distributions. Open the terminal and type man nome_da_funcao, example: man scanf

  • Thank you Skywalker ;) I get it, I’ll try it this way. Cool, I’ll look it up on the linux terminal. Very interesting.

2


If you want to ensure that the user only type numbers you must do the treatment of scanf as follows:

#include <stdio.h>
#include <stdlib.h>

int clean_stdin()
{
    while (getchar()!='\n');
    return 1;
}

int main(void)
{
    int num =0;
    char c;

    printf("\nDigite um numero: ");
    if (( scanf("%d%c", &num, &c) != 2 || c != '\n' ) && ( clean_stdin()) )
        printf("\nVoce deve digitar somente numeros.\n\n");
    else
        printf("\nNumero digitado: %d\n\n", num);

    return 0;
}

If the user types a string as 21dff then you’ll have to clean up your stdin, the function clean_stdin() does it for you.

Explanation

Example 1: If the user type abcd and then ENTER the scanf had returned 0 and nothing will be captured.

Example 2: If the user type 40 and then ENTER the scanf returns 2 or two elements were captured, and "%d" received the value 40 and "%c" received "\n".

Example 3: If the user type 32abc and then ENTER, the scanf returns two elements, and "%d" received the value 32 and "%c" received "a".

So you can ensure that the user only type numbers.

Research source here.

  • Thanks Dener, this explanation will help me now. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.