G. Cata
G. Cata

Reputation: 13

c: last loop in if elseif else is not working

I'm trying to solve an error on the loop in code below but I can't figure out why it won't go again in first "if", after in last "else" ("Introduceti d sau n! "), I write 'n'. The 'd' is working fine and going back to "else if". In the last revised code, I've updated with the working code. At least, it works after I press 'n' in last "else" sentence and is taking me back to first menu.

Original code

switch(alegere_opt1)
{
    case 1:
        while(decizie != 'n')
        {          
            alegereStudent(&stud);
            printf("Doriti sa introduceti un nou student? (d/n):");
            scanf(" %c",&decizie);
            if (decizie == 'n')
                {
                meniuPrincipal();
                alegereStudent(&stud);
                printf("Doriti sa introduceti un nou student? (d/n):");
                scanf(" %c",&decizie);
                }
            else if (decizie == 'd')
                {
                alegereStudent(&stud);
                printf("Doriti sa introduceti un nou student? (d/n):");
                scanf(" %c",&decizie);
                }
            else
                {
                printf("Introduceti d sau n! ");
                scanf(" %c",&decizie);
                }
        }
        break;

Revised code:

switch(alegere_opt1)
{
case 1:
{           
            alegereStudent(&stud);
            printf("Doriti sa introduceti un nou student? (d/n): ");
            scanf(" %c",&decizie);
                 if (decizie == 'n')
                    {
                    meniuPrincipal();
                    alegereStudent(&stud);
                    }

                   else if (decizie == 'd')
                    {
                    alegereStudent(&stud);
                    }
                    else
                     {
                    printf("Introduceti d sau n! ");
                    scanf(" %c",&decizie);
                     }
}
break;

Revised code correct:

    switch(alegere_opt1)
{
     case 1: while (decizie != 'z')
     {          
                {          
            alegereStudent(&stud);
            printf("Doriti sa introduceti un nou student? (d/n):");
            scanf(" %c",&decizie);
            if (decizie == 'd')
                {
                alegereStudent(&stud);
                printf("Doriti sa introduceti un nou student? (d/n):");
                scanf(" %c",&decizie);
                }
            else if (decizie == 'n')
                {
                meniuPrincipal();
                }
            else
                {
                printf("Introduceti d sau n! ");
                scanf(" %c",&decizie);
                    if (decizie == 'n')
                    {
                    meniuPrincipal();
                    }
                }

                }
     }
        break;

Upvotes: 0

Views: 189

Answers (1)

suvojit_007
suvojit_007

Reputation: 1728

I can't figure out why it won't go again in first "if", after in last "else" ("Introduceti d sau n! "), I write 'n'. The 'd' is working fine and going back to "else if".

Because of your while loop condition(while(decizie != 'n')). Loop will execute as long as you give anything but n as input. It won't go in the first if as you're giving n as the input.

Upvotes: 2

Related Questions