Reputation: 13
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.
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;
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;
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
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