Reputation: 29
I want to use the comparison of two strings as a condition of a cycle, but the cycle goes into infinite loop and I do not know why.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void input(int a[], char *comando, char *aux) {
fgets(aux, 35, stdin);
sscanf(aux, "%s %d %d", comando, &a[0], &a[1]);
}
int table_size=10;
int ar_temp[2];
char comando[2];
char aux[35];
int main() {
while((strcmp (comando, "X"))!=0 || (strcmp (comando, "x")!=0)) {
input(ar_temp, comando, aux);
}
return 0;
}
Help me please
Upvotes: 1
Views: 106
Reputation: 881153
While (I am not a llama) or (I am not a dog) ...
Even llamas and dogs will have a true result for one of those sub-clauses so, since true or anything is true, the statement will always result in true.
What you need instead is (reverting back to your actual code rather than my slightly ridiculous example):
while ((strcmp (comando, "X") != 0) && (strcmp (comando, "x") != 0)) {
// ^^
// and rather than or
You'll notice I've also "fixed" your first clause so that the use of parentheses is consistent, moving the )
to after the 0
.
Of course, if you have any other places where you check comando
, it may be worth just making it single case up front, then you only ever need to check the lower case variant.
Upvotes: 0
Reputation: 2534
The condition is wrong, the while statement always evaluated to true.
while((strcmp (comando, "X"))!=0 || (strcmp (comando, "x")!=0))
It should be:
while((strcmp (comando, "X"))!=0 && (strcmp (comando, "x")!=0))
Upvotes: 3