views:

63

answers:

4

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 im 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();

    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;
}
+1  A: 

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.

Himadri
+4  A: 

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.

strager
A: 

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;
}
Vijay Mathew
Global variables aren't *always* bad. He's learning; relax. Your rewrite may cause too much confusion, too; it's introducing `getchar`, `fflush` (which is ugly), `EOF`, and `ctype.h` and functions.
strager
@strager OK, he can use them when he is weaned :-)
Vijay Mathew
A: 

Thank you friends, im actually a beginner here, thanks for bearing with the silliness of my question. Also, Thanks strager, i'll explore more of scanf.

Thanks Himadri, i really did miss single quoting the digits condition.

Thanks Vijay, you suggested a really good improvisation to the code and it was very resourceful.

Arun