user302686
user302686

Reputation: 31

How to check whether input is letter in C?

Hello this part of my code. It checks if entered value is in base 2 or not. I work when I enter integer value. But I want to get the code to check for letter character inputs. How can I do that?

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

int main()
{
    int DataForBase1,DataForBase1A,CheckForBase1;

    printf("For disk1 enter data in base 2: ");
    scanf("%d",&DataForBase1);

    DataForBase1A=DataForBase1;

    while(DataForBase1!=0)
    {
        CheckForBase1=DataForBase1%10;
        if( (CheckForBase1!=0) && (CheckForBase1!=1) ) 
        {
            printf("ERROR: This is invalid input for base 2\n");
            printf("For disk1 enter data in base 2: ");
            scanf("%d",&DataForBase1);
        }
        else
            DataForBase1=DataForBase1/10;       
    }

    system("pause");
    return 0;
}

Upvotes: 0

Views: 261

Answers (3)

Ashwini Singh
Ashwini Singh

Reputation: 127

Use a %c just after the %d in scanf().
For example:

int DataForBase1,DataForBase1A,CheckForBase1;
char ch;

printf("For disk1 enter data in base 2: ");
scanf("%d%c",&DataForBase2, &ch);

This way,

  • When you will enter numbers like 11d, 63f etc., the %c will eat-up the extra character after number and
  • On entering pure numerical values like 11, 63 etc. %c will hold the \n character. Thus your program will work just fine.

PS.: Input assumption based on your another 'just-deleted' post, where you had posted the same code.

Upvotes: 1

ron
ron

Reputation: 995

consider this http://www.tutorialspoint.com/c_standard_library/ctype_h.htm

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

#include <string.h>
#include <ctype.h>

#define MAX   80   /* max characters to read in */


int Parse_String ( char str[], int *DataForBase1 )
 {
       /* do all checking on user input */
       /* use functions from ctype.h  and  string.h */
       /* ctype.h will have functions to allow checking if number or character */

       int i, len, value;
       int result = 1;

       *DataForBase1 = -1;

       len = strlen( str );
       for ( i = 0; i < len; i++ )
       {
           if ( ! isalnum( str[i] )
           {
              result = 0;
              break;
           }
        }

        /* write another for loop here checking every character is either 0 or 1 */
        /* and if any is not then set result = 0 and handle accordingly */

        i = sscanf( str, "%d", &value );
        if ( i != 1 )
        {
           *DataForBase1 = -1;
            result = 0;
        }
        else
        {
            *DataForBase1 = value;
        }

       return result;
 }

int main()
{
    char str[MAX];
    int result;
    int DataForBase1,DataForBase1A,CheckForBase1;

    printf("For disk1 enter data in base 2: ");
    fgets( str, MAX, stdin );

    result = Parse_String( str, &DataForBase1 );

    if ( result == 1 )
       DataForBase1A=DataForBase1;
    else
    {
       /* handle error condition here */
    }

    while(DataForBase1!=0)
    {
        CheckForBase1=DataForBase1%10;
        if( (CheckForBase1!=0) && (CheckForBase1!=1) ) 
        {
            printf("ERROR: This is invalid input for base 2\n");
            printf("For disk1 enter data in base 2: ");
            scanf("%d",&DataForBase1);
        }
        else
            DataForBase1=DataForBase1/10;       
    }

    system("pause");
    return 0;
}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

You can tell that the characters at the current input position do not represent a number you could do this:

int count = scanf("%d", &DataForBase1);
if (count == 0) {
    // Nothing was read
    ...
}

If you detect that scanf has returned zero, you cold read and ignore a string, print an error message, and loop back to read a numeric value again.

Upvotes: 0

Related Questions