dimanche 5 décembre 2021

both of the printf statements inside the while loop gets printed simultaneously [duplicate]

#include<stdio.h>
#include<math.h>

int main(){
    char x;
    int i=1,n,h,p,tot=0;
    printf("Enter the number of vehicles to be parked:");
    scanf("%d",&n);
    // using while loop to get the values for the number of vehicles
    while(i<=n){
        printf("\nEnter c for car,b for bus, t for 2 wheelers:");
        scanf("%c",&x);
        printf("\nno of hours:");
        scanf("%d",&h);
        if(x=='c'){
            p=10*h;
            printf("\n%d rupees for %d hour",p,h);
            tot=tot+(10*h);
        }
        else if(x=='b'){
            p=20*h;
            printf("\n%d rupees for %d hour",p,h);
            tot=tot+(20*h);
        }
        else if(x=='t'){
            p=5*h;
            printf("\n%d rupees for %d hour",p,h);
            tot=tot+(5*h);
        }
        else{
            break;
        }
    
        i=i+1;
    }
}

this code is not working as expected. before executing the scanf statement it executes the next printf statement. it executes both printf statement and then executes scanf statement.

Aucun commentaire:

Enregistrer un commentaire