I'm trying to compare something that i read with fgets with the words from the first column of the array, but I can't get it, I thing it could be the \0 at the end of each string but I can't get the comparison works
#include <stdio.h>
#include <string.h>
#define MAX_STRLEN 3
const char *bdato [][columns]={
{"tc","Torta Cubana"},
{"th","Torta Huatulco"},
{"tm","Torta Mexicana"},
{"tr","Torta Rusa"},
{"r", "Refresco"},
{"a", "Agua sabor"}};
int total() {
char v[20];
int flag=0,cont=0;
fgets(v,sizeof(v),stdin);
do {
if(strcmp(v,bdato[cont][0])==0){ /*this*/
flag=1;
printf("I found one");
}
cont++;
} while(!(flag==1||cont==5));
}
the rewritten code:
#defines .....
.............
.............
int total(){
size_t len = strlen(v);
printf("something here");
fgets(v,sizeof(v),stdin);
len = strlen(v);
if(len>0){
v[len - 1] = '\0';}
if(strcmp((v,bdato[cont][0])==0)){
/*another code*/
}
}
fgetsincludes a newline. Also, you appear to have an extra closing brace.