Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why is it not recognising nested if ?

+3 votes
asked Jun 13, 2021 by Vishal P Damodar (190 points)

    printf("Do you have a laptop ? : ");
    scanf("%s",&lap);
    if(lap == 'y')   // (Y/y) answer
    {
        printf("How many do you have ? : ");
        scanf("%d", &no);
        if(no == '1')       // no. of laptop like 1, 2, 3 .
        {
            printf("How much did you pay ? : ");
            scanf("%d", &lpr1);

        }
 

3 Answers

0 votes
answered Jun 30, 2021 by Prathamesh Moharkar (140 points)
printf("Do you have a laptop ? : ");
    scanf("%s",&lap);
    if(lap == 'y')   // (Y/y) answer
    {
        printf("How many do you have ? : ");
        scanf("%d", &no);
        if(no == '1')       // no. of laptop like 1, 2, 3 .
        {
            printf("How much did you pay ? : ");
            scanf("%d", &lpr1);

        }
        
    } // You missed this shit bro :)
0 votes
answered Jul 1, 2021 by 238 VUPPALA SATHWIK (140 points)

Well the problem is when you use if conditional statement then in test expression

if (test expression) 
{
   // code
}

when you wanna check it with number the test expression is (no == 1) the single quotes are only used to check the condition for letter like (lap == 'y')
+2 votes
answered Jul 3, 2021 by xDELLx (10,500 points)

The first if looks funny to me !!. You are comparing a string with " == " operator to a char data type.
My guess is even the first if evaluates as false.Since base address of lap is compared to 'y' ,ASCII code 121.

Now coming to the nested if ,again your mixing types when comparing them. variable 'no' is int & '1' is char , but compiler will convert it ( '1' ) to its ASCII format ie to 49 .In short '1' will evaluate as 49 when comparison happens!!.

commented Jul 5, 2021 by Peter Minarik (84,720 points)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...