简体   繁体   English

如何检查C中是否输入字母?

[英]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. 它检查输入的值是否以2为底。 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;
}

Use a %c just after the %d in scanf() . scanf()%d之后使用%c
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 当您输入11d,63f 数字时, %c将吞噬数字后的多余字符,
  • On entering pure numerical values like 11, 63 etc . 输入纯数值,例如11、63 %c will hold the \\n character. %c将保留\\n字符。 Thus your program will work just fine. 因此,您的程序将正常运行。

PS.: Input assumption based on your another 'just-deleted' post, where you had posted the same code. PS .:根据您在另一篇“刚刚删除”的帖子中输入的假设,您已在其中发布了相同的代码。

consider this http://www.tutorialspoint.com/c_standard_library/ctype_h.htm 考虑一下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;
}

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. 如果检测到scanf返回了零,则冷读取并忽略字符串,打印错误消息,然后循环返回以再次读取数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM