kaweesha marasinghe
kaweesha marasinghe

Reputation: 21

If condition is not working inside a user defined function in c

I wanted to check character variable (type a_basis) is equal to H or F to assign values for total variable but if condition is not working inside the defined function, it always output the value of total is 0.00000

I have tried checking the int variable 1st and then char but the result is same. Arguments pass into the functions perfectly but thif condition statement is not working

#include<stdio.h>

double calculate(int typeOfRoom, char a_basis){
    double total;

    if (typeOfRoom==1){
    
                if(a_basis=='F' || a_basis=='f'){
                    total = 25555.00;
                    
                }
                else if(a_basis=='H' || a_basis=='h'){
                    total = 17250.00;
                    
                }
                
    }
    else if (typeOfRoom==2){
    
                if(a_basis=='H' || a_basis=='h'){
                    total = 17500.00;
                    
                }
                else if(a_basis=='F' || a_basis=='f'){
                    total = 12250.00;
                    
                }
                
            }
    else if (typeOfRoom==3){
    
                if(a_basis=='F' || a_basis=='f'){
                    total = 9000.00;
                    
                }
                else if(a_basis=='F' || a_basis=='f'){
                    total = 7250.00;
                    
                }
                
            }
                
            
            return total;
}


int main(void){
    
    int typeOfRoom,no_days;
    char crd_type;
    char a_basis;
    double total;
    
    
    
        while(typeOfRoom !=(-1)){
        
            printf("Enter Room type :");
            scanf("%d",&typeOfRoom);
    
            printf("Enter Accommodation Basis (F/H) :");
            scanf("%c%*c",&a_basis);
            
            total = calculate(typeOfRoom,a_basis);
            printf("%lf",total);
    
    }
    
    
    return 0;
}

Upvotes: 0

Views: 145

Answers (1)

kaweesha marasinghe
kaweesha marasinghe

Reputation: 21

printf("   >Enter accomadation Basics :");
    scanf(" %c%*c",&boardType); 
printf("   >Enter Card Type(G,S,B)");
    scanf(" %c%*c",cardType);

Thanks to @Jonathan Leffler I have found that my issue was on scanf() functions which leaves and \n character that's why it doesn't check inside the if conditions or switch conditions. My scanf() functions should be corrected as above then it will work perfectly`

Upvotes: 1

Related Questions