#include <stdio.h>
void clearKeyboard(void)
{
while (getchar() != '\n') ; // empty execution code block on purpose
}
int yes(void)
{
char a,b;
printf("<Please enter a character>: ");
scanf("%c%c", &a,&b);
while ((a !='Y' && a !='y' && a !='N' && a!='n') || (b!='\n'))
{
if (b!='\n') ungetc(b, stdin),scanf("%*[^\n]%c", &b);
a='n',b='\n';
clearKeyboard();
printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
scanf("%c%c", &a,&b);
}
if (a =='Y' || a=='y')
{
printf("Contact Management System: terminated\n");
return 1;
}
else
{
if (a =='N' || a=='n')
ContactManagerSystem();
return 0;
}
}
int menu(void)
{
int i;
printf("0. Exit\n\nSelect an option:> ");
scanf("%d", &i);
while (i<0 || i>6)
{
printf("*** OUT OF RANGE *** <Enter a number between 0 and 6>: ");
scanf("%d", &i);
}
return i;
}
void ContactManagerSystem(void)
{
int i=menu();
switch(i)
{
case 0 :
printf("Exit the program? (Y)es/(N)o: ");
yes();
}
}
int main(void)
{
ContactManagerSystem();
}
So my "yes" function is working fine on it's own but when I call it within
ContactManagerSystem()의 경우 그것은 어떤 이유로 "예"기능에 미친거야 0 실제로 내가 같이 "아니오" "예"또는 입력하도록 필요 유효성 검사 함수이다 ' Y'''N '또는'n ' 사용자가 "Y", "y", "N"또는 "n"을 입력 할 때까지 기다리십시오 (예 또는 아니오). 사용자가 예를 대답하는 경우 ("Y"는, "Y")는 다음과 같은 메시지가 표시하는 프로그램이 종료됩니다 :기능 점점 쓰레기 값
연락처 관리 시스템은 다음 경우, 그렇지 않은 경우 (줄 바꿈 다음) < 을 종료 사용자가 아니오 ("N", "n")를 입력하면 응용 프로그램은 계속 메뉴를 표시합니다.
괄호를 사용하십시오. 'a'가''y'' (그리고 리턴 키가 뒤따라야 함)로 설정되면''Y'' 또는''n''' 또는''N'''과 같지 않기 때문에'|| '조건은 참으로 평가됩니다. 실제로'yes'라고 타이핑하면 다른 문제가 생깁니다. 그리고 EOF가 비난받을만한 행동으로 이어지는 것을 나타냅니다. –
'a! = X || a! = Y' (그리고'X! = Y')는 항상 참입니다. –
'clearKeyboard'가 필요 없습니다. – jxh