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

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

/ * opHelp is to receive an integer, but I want to do an if here,  saying that if the person writes a character rather than integer, it goes to the help screen and shows the message that is inside the if. And then the person has to re-enter, in this 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 numerical value, the program works fine, but when I type some character (other than the special ones I already have defined and already made the ifs for it) then when I type a character it is in an infinite loop . It happens that when I did the test to see what I was printing when I typed a character it prints a NULL value, I tried to represent that value in if but I can not. Can anyone help me?

I want to do this because I am 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 should it happen.

    
asked by anonymous 31.10.2015 / 17:05

2 answers

2

If you want to ensure that the user types only numbers, you should treat 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 will have to wipe your stdin , the clean_stdin() function does this for you.

Explanation

Example 1 : If the user types abcd and then ENTER the scanf will return 0 and nothing will be captured.

Example 2 : If the user type 40 and then ENTER the scanf will return 2 or two elements were captured, and "%d" was given 40 e "%c" received "\n" .

Example 3 : If the user types 32abc and then ENTER , scanf will return two elements, and "%d" received 32 and "%c" received "a" .

So you can make sure the user types only numbers.

Search source .

    
31.10.2015 / 18:56
2

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

int res=0, in=0;

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

For your problem the solution would be to create a loop being the output condition return 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   operational system. As soon as you have any questions regarding any   function in C you can take a look at the document, using the    man program already installed on all linux distributions.   Open the terminal and type man nome_da_funcao , example: man scanf

    
31.10.2015 / 18:16