Click here to Skip to main content
15,909,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm beginner in C language.I have a problem with logic.For example, the availability of the car is 1,but when i insert the amount of the car i want to rent is more than 1, the system is still running and does not enter the if else statement.Thanks !
C#
int car;
    int amountcarSAGA;
    int amountcarVIOS;

        printf(" Which car you prefer\n\t1 PROTON SAGA %d",availabilitySAGA);
        printf("\n\t2 TOYOTA VIOS\n\t");
        scanf("%d", &car );

        switch (car)
        {
          case 1 : if(availabilitySAGA<=0)
                   {
                       printf("No car available");
                       break;
                   }
                   else
                   {


                   printf("How many days you want to rent\n\t");
                   scanf("%d", &day);
                   printf("How many cars you want to rent\n\t");
                   scanf("%d",&amountcarSAGA);

                   if(availabilitySAGA>0)
                   {
                      calculation_carSAGA(day,amountcarSAGA);
                   }
                   else
                   {
                    printf("Amount of car exceed the availability");
                   }
                   availabilitySAGA=availabilitySAGA-amountcarSAGA;
                   break;
                   }

         case 2 : printf("How many days you want to rent\n\t");
                  scanf("%d", &day);
                  printf("How many cars you want to rent\n\t");
                   scanf("%d",&amountcarVIOS);
                   calculation_carVIOS(day,amountcarVIOS);
                   break;
         default: printf("\nEnter only one from the above");

        }
Posted
Updated 21-Dec-13 17:02pm
v2
Comments
ZurdoDev 21-Dec-13 22:21pm    
Debug it and find out what is happening.
arave0521 21-Dec-13 22:31pm    
User can insert the amount of car more than the availability of car.
However,for second input user cannot enter the case because the amount car is less or equal than 0.
ZurdoDev 21-Dec-13 22:38pm    
Why? Just put a breakpoint and see what is happening.
arave0521 22-Dec-13 0:26am    
problem solved
thanks !!! :)

1 solution

My Friend,
This is an obvious logic that user can enter 100 cars even if you only have 3 cars. The reason is explained below... You may have to read :P

1. The first if condition as per given by you is :
C
if (availabilitySAGA > 0) { // implies that SAGA is avialable even if one the condition is true
    // Rent the car to user
} else { // Now this condition is already checked by you previously before entring this if else condition am I right?
}

This condition thereby is completely wrong. The Logic is wrong.

2. Now, the logic should be this
-> Check if AvailabilitySAGA == 0 'cause you are not going to go in minus can you?
-> Compare the AvaialabilitySAGA >= Entered amount; This is the correct logic.
-> Do whatever you want based on the positivity of the condition.

3. You generally should try to do a paper-pen simulation i.e. making flow charts and pseudo codes, before writing any logic. It will reduce considerable red eyes due to excessive debugging by you all night :P

I tried to put it in a fun way... Hope that it helped although I had read your comment already that you solved the problem yourself. It's really good. :)

With Regards
Tushar Srivastva
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900