-
Endless loop!
Why this do-while loop never ends?? even if I type s or n, it doesnt end and return the value of eleccion.
char otravez()
{
char eleccion;
do{
printf("Jugar otra vez?(s/n)");
eleccion=getche();
}while(eleccion!='s'||eleccion!='n');
return eleccion;
}
-
while(eleccion!='s'||eleccion!='n');
This line should be using the logical AND operator
while( eleccion != 's' && eleccion != 'n' );
What you are saying is while character doesn't equal s or doesn't equal n continue. Well one of those is always going to be true, therefore OR lets one condition true and one false = true in the end. AND requires both conditions be true to return true. Hope that makes sense.
-
Hi, try this:
Code:
char otravez()
{
char eleccion;
do{
printf("Jugar otra vez?(s/n)");
eleccion=getchar();
}while(eleccion='s'|| eleccion='n');
return eleccion;
}
This would end the loop if 's' or 'n' is inputted. Someone correct me if i'm wrong so as i may learn.
-
Quote:
Originally posted by loopy
Hi, try this:
Code:
char otravez()
{
char eleccion;
do{
printf("Jugar otra vez?(s/n)");
eleccion=getchar();
}while(eleccion='s'|| eleccion='n');
return eleccion;
}
This would end the loop if 's' or 'n' is inputted. Someone correct me if i'm wrong so as i may learn.
Well first of all in your while condition you are assigning the variable eleccion to the character s and n. You need to equal signs for comparison ( == ). If you add those you are saying if your choice equals s OR your choice equals n , then continue. I don't think that is what he wants.