How to get an exit from for
loop because my code always gets the latest result from the for
loop? What's wrong with my for
loop condition? This is my code:
#include <stdio.h>
#include <string.h>
int main(){
int i, n = 3, a;
char x[20];
struct data {
char nim[10];
};
struct data batas[100];
printf("TEST1 : "); scanf("%[^
]s", batas[0].nim);
printf("TEST2 : "); scanf(" %[^
]s", batas[1].nim);
printf("TEST3 : "); scanf(" %[^
]s", batas[2].nim);
char str[10];
printf("TEST : "); scanf(" %[^
]s", str);
for(i=0; i<n; i++){
if (strcmp(str, batas[i].nim) == 0) {
puts("Value exist");
strcpy(x, "FLAG");
} else {
puts("Value doesn't exist");
strcpy(x, "FLAGXX");
}
}
printf("%s
", x);
return 0;
}
Here, from the result of x
always gets the FLAGXX
result. I want to get the FLAG
result here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…