【发布时间】:2018-04-25 01:11:57
【问题描述】:
I have attached output image of my program. you can see the problem i am facing by clicking here
下面的代码是我的程序,它使用我自己版本的内置函数 isalpha() 和 isalnum( )。 不适用于 while 循环的第二次迭代
#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;
}
上面的代码第一次正常工作,但在 while 循环的第二次迭代中。当我将“Y”作为输入代码直接跳转到 scanf 的 user_option 部分时,然后等待“getchar()”- 函数。
即使使用 fflush() 函数填充缓冲区后,我也无法提示程序输入字符。
【问题讨论】:
标签: c while-loop buffer stdin