How to recognize the type of data entered - C

Asked

Viewed 196 times

-2

I am making a program where the user can register, search and remove customers. To enter it type "insert [client name]" and a code is generated automatically. I have a problem in the search function. Currently the user has to type: "search x y" where x is an int q identifies whether the search will be done by the client’s code or name and y is the corresponding value. I wanted to do this without asking the user to specify the type of search. Have some way to receive the data and then check if I received an integer or a string?

  • https://answall.com/help/how-to-ask

1 answer

0


You can use the isdigit function, you will have to go through the entire input with it, if placing the code is easier, but follow an example taken from the help of watcom c.

#include <stdio.h>
#include <ctype.h>

    //Aqui você não necessita por um vetor, imagine que seja o texto que 
     você está armazenando

char chars[] = {
    'A',
    '5',
    '$'
};
#define SIZE sizeof( chars ) / sizeof( char )
int main()
{
    int   i;

    //Faça um for percorrendo a entrada caracter por caracter

    for( i = 0; i < SIZE; i++ ) {
        printf( "Char %c is %sa digit character\n",
                chars[i],
                ( isdigit( chars[i] ) ) ? "" : "not " );
                 //Verifique na função isdigit se é um numero ou não, da 
proxima vez por favor deixar o codigo
    }

    return 0;
}
  • Thanks, I did not put the code because it is a more general case I thought it would not make a difference, in the next I put.

  • Fine, but see if you can, otherwise just give a warning here @Raul Good luck

Browser other questions tagged

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