开发者

Data Validation in C

开发者 https://www.devze.com 2023-01-11 09:49 出处:网络
I was trying out a simple program in C for validating user data. The program is supposed to identify whether a user entered character is a number, alphabet or a special character.

I was trying out a simple program in C for validating user data. The program is supposed to identify whether a user entered character is a number, alphabet or a special character.

Somehow , the code identifies every kind of input character as a number. I have appended the code below, I'd be grateful if someone could kindly point out where I'm going wrong ?

//Program to take input from the user and determine whether it is character, number, or a special character

#include<stdio.h>
#include<conio.h>
#include<string.h>


    char ch;

int main()

{
     clrscr();

开发者_如何学C    printf("Enter a character \n");
    scanf("%c \n",ch);

    if ((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z') )
    {
        printf("The character entered is an alphabet \n" );

    }
     else if ((ch>=0)&&(ch<=9))
    {
        printf("Character entered is an number \n");
    }


    else
    {
        printf("Character entered is a special character");

    }
    return 0;
}


scanf accepts a pointer as the argument for %c. In other words,

scanf("%c \n",ch);

should be written as:

scanf("%c\n",&ch);

Without the reference operator (&), scanf receives the value of ch. In this case, the value is garbage, because ch is unset.* Referencing ch gives scanf a pointer to ch, not ch itself, so scanf can modify the value of ch by dereferencing the pointer (using the dereference operator, *).

There's also the issue with digit checking that Himadri mentioned.

* This is actually undefined behaviour.


Oh, Arun very silly mistake. In your second condition in else if you have to right 0 and 9 in single quotation mark.

So, your code will be -

if ((ch>='A'&& ch<='Z')||(ch>='a'&& ch<='z') )
{
    printf("The character entered is an alphabet \n" );

}
 else if ((ch>='0')&&(ch<='9'))
{
    printf("Character entered is an number \n");
}
else
{
    printf("Character entered is a special character");

}

May be this is the only mistake. Now, it should work.


A few comments on style:

  1. conio.h and clrscr() are non-standard.
  2. Global variables are bad (char ch). Declaring them non-static is also bad.
  3. Always check the return value of scanf. This will help you catch input format errors. In this case, as we need to just a single character, getchar is more appropriate.

This is how I would've written this program:

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

int main()
{
  int ch; /* We use an int because it lets us check for EOF */
  printf("Enter a character: ");
  fflush(stdout); /* Remember to flush the output stream */
  ch = getchar();
  if (ch == EOF)
    {
      printf("end-of-file or input-error\n");
      return 1;
    }
  if (isalpha(ch))
    printf("The character entered is an alphabet\n" );    
  else if (isdigit(ch))
    printf("Character entered is an number\n");    
  else
    printf("Character entered is a special character\n");    
  return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消