Link to home
Start Free TrialLog in
Avatar of iamnovice
iamnovice

asked on

How to distiguish between characters and integers in c?

I declare an integer. now if i enter a character by mistake i want to flag off an error. how do i go about doing this i.e. make c recognise that i have inputted a character and not an integer and flag off that error. Please help....
ASKER CERTIFIED SOLUTION
Avatar of Laminamia063099
Laminamia063099

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Laminamia063099
Laminamia063099

What you can do is the following:
Read the input as a character, then use the c function: isalpha ( char_input ) to check to see if the input was a character.  If so, then output to the user that they entered an invalid input and you want an integer.  if it isn't a character, or

if (!isalpha ( char_input ))

then use:

int x;
//get character, check character...
x = atoi ( char_input );

That will convert the character to it's corresponding integer.

Laminamia

Ask if you want to see more code and I'll be happy to show the full example :)
#include <stdio.h>
#include <ctype.h>

char char_input;
int x;

scanf ( "%c", &char_input );

//loop until they input a number, not a character:
while (isalpha ( char_input ))
  {
  //character was input
  printf ("Error: Input an integer please");
  scanf ( "%c", &char_input );
  }

//they entered a valid number, convert to integer for use in program:
 
x = atoi ( &char_input );  //send the (char *) character pointer to atoi.
//atoi takes a string, which is just a character pointer.
//The "&" gets the pointer or address of a variable to
//send it to a function that wants a pointer or address
//instead of the variables value, like in the scanf calls.
Do you mean you are reading some input and tying to place it into an integer? Or are you assigning something to the integer from within your own code?

If you are reading in some data there only way to do it is to read the data as a string, and convert it to an integer - flagging the input if it cannot be converted.

The following code shows you an example:
/*
 * string to int
 * c code to demonstrate converting a string to an integer
 */
#include <stdio.h>

int main(int argc, char *argv[])
{
        char *str_array;
        int my_int;
        if (argc < 2)
        {
                printf("usage: %s string_to_convert\n", argv[0]);
                return 1;
        }
        str_array = argv[1];
        printf("Argument is: %s \n", str_array);
        if (! isdigit(str_array[0]))            /* check the first char
                                                 * if it is a char then
                                                 * string is not numeric
                                                 */
        {
                printf("Not an integer!\n");
                return 1;
        }else{
                my_int = atoi(str_array);
                printf("The integer is: %d\n",my_int);
        }
        return 0;
}

This code is called with an string arguement that is converted to an integer if the FIRST character is a valid digit (ie 0..9).

The function atoi does all the work for us, converting the first string of digits it finds into a integer. We just put a proviso on this by stipulating that the first character must be a digit. We do this becasue if atoi does not find any valid digits in the string it will assign an integer value of zero. This would make input such as "abc" and "0" give the same integer values.

I hope this helps.

Bron.
Laminamia: you've missed out the case of the user typing a special character, like !"£$%^*... etc.

To check to see if the character that they entered is a special character just check this first:

isalnum ( char_input ));  

That will check if a character is an alphanumeric (a-z, A-Z) or a digit.  Then check to see if it's a letter or number with isalpha.

Using atoi without checking the character to see if it's a letter or number won't allow the user to enter the value zero because it will be an ambigous result from the atoi () call.
 
You are correct on the string (except for the value 0, which exception you mentioned), I was answering for the case of a character, as the question stated.
bronwyn_black's example is a more robust example, I admit happily.  My example, though basic, would work for the case as I read the question.  Nice code bronwyn.  :)

The only downfall of the c language is it's lack of error checking.  This is an important improvement made in C++.  I think the lack of error checking will always haunt us with code much more complicated (though not overly so) that it would have to be if error checking had been implemented, as in ADA and C++.

Laminamia


#include <stdio.h>

void main(){
int what_is_it;
 printf("\n     HIT A KEY: ");
 __asm{
  mov AH, 1
  int 21h
  mov BYTE PTR what_is_it, AL
 }
  if(what_is_it < 48 || what_is_it > 57)printf("\n     YOU DID NOT ENTER AN INTEGER.  YOU ENTERED %c.",what_is_it);
  if(what_is_it >= 48 && what_is_it <= 57)printf("\n     YOU ENTERED %i",what_is_it -48);
}
Laminamia, you're too kind. Cheers :)