gpt4 book ai didi

c - C 中带条件的 Goto 语句

转载 作者:行者123 更新时间:2023-11-30 16:56:15 25 4
gpt4 key购买 nike

如何使用 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,我不明白为什么。

最佳答案

您没有清除输入缓冲区,请尝试以下操作:

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 ;

关于c - C 中带条件的 Goto 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com