小编Hel*_*y98的帖子

while 循环代码不起作用(keepgoing='y')

所以我正在学习如何在 C 中使用 while 和 for 循环,但这段代码似乎不起作用。scanf 语句似乎被忽略了,循环只是重复自己而不需要我输入“Y”来重复。这是代码:

void showCommission();

void main() {
    char keepGoing='y';
    while(keepGoing=='y') {
        showCommission();
        printf("Do you want to calculate another?\n");
        scanf("%c",&keepGoing);
   }
}

void showCommission() {
    float sales,commission;
    const float COM_RATE=0.10;
    printf("Enter the amount of sales\n");
    scanf("%f",&sales);
    commission=sales*COM_RATE;
    printf("The commission is $%f.\n",commission);
}
Run Code Online (Sandbox Code Playgroud)

这是运行代码给我的内容:

Enter the amount of sales                                                                         
5000                                                                                              
The commission is $500.000000.                                                                    
Do you want to calclulate another?    

...Program finished with exit code 10                                                             
Press ENTER to exit console.  
Run Code Online (Sandbox Code Playgroud)

它从不提示我输入 y 并且代码只是出于某种原因退出。

c loops scanf while-loop

1
推荐指数
1
解决办法
216
查看次数

标签 统计

c ×1

loops ×1

scanf ×1

while-loop ×1