如何使用if和else条件执行goto语句 如果他/她想要重新开始,我希望我的用户做出决定
loop1 :
printf("how many dagre entering ? \n");
scanf("%d", &NumOfGrade);
for ( i = 0 ; i < NumOfGrade ; i ++ ) {
printf("\nenter the grade\n");
scanf("%d", &grade);
totalGrade += grade ;
if ( grade < 12 )
failsCount ++;
}
ave = (float) totalGrade / NumOfGrade ;
printf("the ave of ur grade are : \n");
printf("%.2f\n", ave);
printf("%d subj fails\n", failsCount);
printf("do you want to startover ? (Y or N) \n");
ans = getchar();
if ( ans == 'Y')
goto loop1 ;
它不会循环1,我不明白为什么。
答案 0 :(得分:0)
你没有清除输入缓冲区,试试这个:
loop1 :
printf("how many dagre entering ? \n");
scanf("%d", &NumOfGrade);
for ( i = 0 ; i < NumOfGrade ; i ++ ) {
printf("\nenter the grade\n");
scanf("%d", &grade);
totalGrade += grade ;
if ( grade < 12 )
failsCount ++;
}
ave = (float) totalGrade / NumOfGrade ;
printf("the ave of ur grade are : \n");
printf("%.2f\n", ave);
printf("%d subj fails\n", failsCount);
printf("do you want to startover ? (Y or N) \n");
__fpurge(stdin); // clear your input buffer, on window, try fflush(stdin)
ans = getchar();
if ( ans == 'Y')
goto loop1 ;