I have attached output image of my program. you can see the problem i am facing by clicking here
Below code is my program to read an input from an user as character and check whether it is an alphabet or an number using my_isalpha() and my_isalnum() functions of my own versions of built in function isalpha() and isalnum(). Not working for second iteration of while loop
#include <stdio.h>
#define NUM 1
#define ALPHA 2
#define ASCII 3
#define BLANK 4
int main()
{
char option;
do
{
//declaration of function
char character;
int user_option, status;
//get the character from the user
printf("Enter the Character:");
//clears both buffers to read the character
fflush(stdin);
fflush(stdout);
//reads one character at a time
character = getchar();
//prompt the user for the option to check
printf("Choice Below Option\n");
printf("1.isalnum\n2.isalpha\n");
printf("Enter your Option:");
scanf("%d", &user_option);
//validation of the user_option
switch(user_option)
{
case 1:
status = my_isalnum(character);
if(status == NUM)
{
printf("Character '%c' is an number", character);
}
else
{
printf("Character '%c' is not a number", character);
}
break;
case 2:
status = my_isalpha(character);
if(status == ALPHA)
{
printf("Character '%c' is an Alphabet", character);
}
else
{
printf("Character '%c' is not Alphabet",character);
}
break;
default:
puts("Invalid Choice.....");
}
printf("\nDo you want to continue?[Y/N]:");
scanf(" %c", &option);
}while (option == 'Y' || option == 'y');
fflush(stdin);
fflush(stdout);
return 0;
}
//Function chaecks for the Number
int my_isalnum(char character)
{
return (character >= '0' && character <= '9')? NUM : -1;
}
//functionn checks for the alphabets
int my_isalpha(char character)
{
return (character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z') ? ALPHA: -1;
}
above code works properly for the first time but during second iteration of while loop. when I give "Y" as an input code dirrectly jumps to user_option part of scanf .rather then waiting for "getchar()"- function.
Even after filushing the buffer using fflush() function i am not able to prompt the program for the character input.