-1

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.

2 Answers2

1

Flushing stdin is undefined behavior. Flushing is meant for output stream not input stream.

As per standard §7.21.5.2

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

Also int getchar(void) : get char() returns an int.

Quickfix: Remove fflush(stdin) from code.

printf("\nDo you want to continue?[Y/N]:");
     scanf(" %c", &option);
     getchar(); // this dummy getchar() will consume the `\n` from stdin.
 }while (option == 'Y')

Note:

Also as other solutions suggest to use fpurge() is an option but again it is non-standard and non-portable.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Here

//clears both buffers to read the character
fflush(stdin);

fflush() doesn't clear stdin buffer as you expected. fflush() is for flushing output stream like stdout not input stream stdin.

To resolve this issue one way is to use extra getchar() to consume \n character. for example

scanf(" %c", &option);
getchar(); /* dummy getchar */

Also here

char character; /* change the type of character to int */
character = getchar();

getchar() return type is of int not char, you need to change the type of character. From the manual page of getchar()

int getchar(void);
Achal
  • 11,821
  • 2
  • 15
  • 37