0

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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Note that `scanf("%c%*c",&a_basis);` runs into the problem that [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/q/5240789/15168). Put a space before the `%c` — `" %c%*c"`. – Jonathan Leffler Nov 07 '22 at 04:52
  • @JonathanLeffler I changed like that but still its same – kaweesha marasinghe Nov 07 '22 at 04:55
  • I've not debugged, and am not going to debug, your whole program. That's an obvious problem I spotted; I don't know what others there are. You need to debug your own program. If you don't know how to use a debugger, now is a fine time to learn. Or you can use extensive `printf()` statements to track what's going on — I do that more often than using a debugger. But you need to learn how to debug your code. (I've deleted my 'plausible guess' since I'm no longer sure it is plausible — in fact, it was probably wrong; sorry!) But the faulty `scanf()` format is definitely going to cause trouble. – Jonathan Leffler Nov 07 '22 at 05:05
  • You should also verify that each `scanf()` call succeeds by testing the return value: `if (scanf("%d", &typeOfRoom) != 1) { …you've got problems…do not go past Go…do not collect $200… }`. – Jonathan Leffler Nov 07 '22 at 05:09
  • @JonathanLeffler sure ill check it is using debug and printf() methods. but I have tested scanf() functions using printf()s to test whether arguments pass correctly they have worked well but I'll learn and try to debug it – kaweesha marasinghe Nov 07 '22 at 05:24
  • Note that for room type 3, you have `if(a_basis=='F' || a_basis=='f'){ total = 9000.00; } else if(a_basis=='F' || a_basis=='f'){` — the second set of conditions will never evaluate to true because the `if` will have been true. The second 'f' and 'F' should presumably be something else. You should worry about what happens when people enter other values than F or H as the basis. At the moment, the behaviour will not be good. – Jonathan Leffler Nov 07 '22 at 06:10
  • @JonathanLeffler I have found that after debugging my character variable takes value 10 instead of character value do you know anything to do about that, So to check whether my code is working I used that integer value to compare in if condition then it is working but the required character compression cannot be done – kaweesha marasinghe Nov 07 '22 at 12:11
  • The value 10 is equivalent to `'\n'` — so you've ignored my first (surviving) comment about putting a space before the `%c` in `" %c%*c"`. The `scanf()` function is a horribly persnickety function and every detail in the format string counts. – Jonathan Leffler Nov 07 '22 at 14:14
  • @JonathanLeffler thank you so much Mr.jonathan it works successfully I'm a undergraduate student I didn't know about than " %c%*c" thing if you have few minutes can you please explain me more about it or show me a way to learn that – kaweesha marasinghe Nov 08 '22 at 02:27
  • Read the Q&A linked in my first comment — it covers the subject. But the long and the short of it is that `scanf()` reads up to but not including the character that stops the conversion (more precisely, it reads the character and puts it back for the next input to consume). There are three conversion specifiers that do not skip white space — `%c`, `%[…]` (scan sets, which are not a modifier of the `%s` conversion specifier) and `%n`. You are running foul of the fact that `%c` does not skip white space. – Jonathan Leffler Nov 08 '22 at 02:41

1 Answers1

1
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`