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

问题描述 投票:0回答:1

所以我正在学习如何在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);}
c loops while-loop scanf
1个回答
0
投票

while(keepGoing =='y'),'y'输入区分大小写。比较之前,请尝试将输入转换为小写或大写。或者,您可以尝试使用while(keepGoing =='y'|| keepGoing =='Y')

比较
© www.soinside.com 2019 - 2024. All rights reserved.