0

This program was coded to complete one of my university lab-sheets. I don't know how to modify the scanf() statement to correct this error. It seems there is an error with the switch-case too. This is my code.

#include<stdio.h>

//Function main
int main (void)
{
    char roomType,payingMethod,cont;
    int noOfRooms=0,noOfNights=0;
    float discount=0,amount=0;

    while(cont!='n'||cont!='N')
    {
        printf("\n(D-Deluxe|S-Superior Deluxe|C-Club Suits|E-Executive Suits|P-Presidential Suits)\nENTER THE ROOM TYPE:");
        scanf("%c%*c",&roomType);

        if(roomType!='D' || roomType!='S' || roomType!='E' || roomType!='C' || roomType!='P')
        {
            printf("Invalid Room Type!\n");
            return 0;
        }

        printf("\nENTER THE NUMBER OF ROOMS:");
        scanf("%d",&noOfRooms);
        printf("\nNUMBER OF NIGHTS:");
        scanf("%d",&noOfNights);
        printf("\nPAYING METHOD (M-cash|C-credit card):");
        scanf("%c%*c",&payingMethod);

        if(payingMethod=='c'||payingMethod=='C')
            discount=0.9;
        else 
            discount=1.0;

        switch(roomType)
        {
        case 'D':
            amount=31000*noOfRooms*noOfNights*discount;
            break;

        case 'S':
            amount=35000*noOfRooms*noOfNights*discount;
            break;

        case 'C':
        case 'c': 
            amount=50000*noOfRooms*noOfNights*discount;
            break;

        case 'E':
        case 'e': 
            amount=75000*noOfRooms*noOfNights*discount;
            break;

        case 'P':
        case 'p': 
            amount=100000*noOfRooms*noOfNights*discount;
            break;

        }

        printf("\nTotal amount payable: %f\n",amount);

        printf("\nDO you want to continue(y/n):");
        scanf("%*c%c",&cont);
        printf("__________________________________________________________");
    }

}//End function main
Asiri Hewage
  • 577
  • 3
  • 16

3 Answers3

0

Nothing is wrong with the inputs. You have a wrong if condition there it must be

if(roomType!='D' && roomType!='S' && roomType!='E' && roomType!='C' && roomType!='P')

because everytime you give a roomType is not all of them D, S, E, C, P so your if condition evaluates to true everytime. You need to use && instead of || so that if its one of them then it evaluates to false.

Dry run of if condition in the question if(roomType!='D' || roomType!='S' || roomType!='E' || roomType!='C' || roomType!='P')

If I give input as D, then roomType!='D' evaluates to false so it jumps to the next check i.e.

roomType!='S' so now my roomType is D and not S, so this condition falls true and it goes inside the if condition and exits the code. This happens everytime so this code never works.

All it need is to modify its if condition

iamsaksham
  • 2,879
  • 4
  • 26
  • 50
0

I'd rather assume that you can read the return value section of the fscanf manual, and write some debugging code to:

  • ensure that scanf is returning the correct value, and...
  • inspect the contents of roomType, payingMethod and cont after you read them (hint: they might be newlines, which would appear invisible on your console when you attempt to print them as characters; so print them as integers instead)...

... than assume that you can't read that manual and fix the problem for you...


In addition to that error, there's a logic error in the termination of your loop. Your loop will run until cond is not equal to 'n' or cond is not equal to 'N'; this is semantically different to when cond isn't contained within the set of ['n', 'N'].

  • When cond == 'n', cond != 'N' so the loop keeps running.
  • When cond == 'N', cond != 'n' so the loop keeps running.

In other words, you have an infinite loop. You probably meant while (cond != 'n' && cond != 'N') or better yet, while (!strchr("nN", cond))...

A similar problem presents itself in if(roomType!='D' || roomType!='S' || roomType!='E' || roomType!='C' || roomType!='P'). You probably meant to use &&, or you could use if (!strchr("DSECP", roomType))...

autistic
  • 1
  • 3
  • 35
  • 80
0

There is wrong if condition instead of using || (or) you have to use &&(and ) because in many cases the if condition become unnecessarily true and return 0 executed and the program get ended with no output. Also use case 'd' and case 's' other than this switch case is perfect . Let's have a dry run of your code In first let's say that if user enter 'S' So roomType='S' now we go to if condition with this value In if roomType!='D' becomes true and as in || expression if any condition is true the result is true so if condition become true and print output Invalid Room Type! and program end by return statement.