#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"、 "y"、 "N"または "n"(はいまたはいいえ)を入力するまで待ちます。 ユーザーがはい(「Y」、」Y」)、それは 次のメッセージ表示プログラム終了します返信した場合:機能取得ごみ値
連絡先管理システム:あれば、そうでない場合<(改行が続く) を終了ユーザーがNo( "N"、 "n")を入力すると、アプリケーションはメニューを表示して に進みます。
括弧を使用します。 'a 'が' 'y''に設定されていて、その後にリターンキーが続くと、' 'Y' 'や'' n ''や ''N' 'と等しくないので、' || '条件は真と評価されます。実際に 'yes'と入力した場合、別の問題が発生します。そして、EOFが非難的な行動につながることを示す。 –
'a!= X || a!= Y'(そして 'X!= Y')は常に真です。 –
'clearKeyboard'は必要ありません。 – jxh