Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to distiguish between characters and integers in c?

Posted on 1999-07-22
11
Medium Priority
?
235 Views
Last Modified: 2010-04-15
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....
0
Comment
Question by:iamnovice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 300 total points
ID: 1263794
There is no error checking in regular c.  This is one of the main downfalls of the language.  If you are using a project environment, Borland or Visual, there may be platform dependant error checking, but for straight up c, you can't.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263795
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 :)
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263796
#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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:bronwyn_black
ID: 1263797
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.
0
 
LVL 2

Expert Comment

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

0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263799
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.
 
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263800
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.
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263801
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


0
 
LVL 1

Expert Comment

by:roaknog
ID: 1263802
#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);
}
0
 
LVL 2

Expert Comment

by:bronwyn_black
ID: 1263803
Laminamia, you're too kind. Cheers :)
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1263804
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question