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 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 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 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 by xDELLx (8,580 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 by Peter Minarik (34,450 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.
...