i experimenting new thing in c convert user input string structure field , obtain value of it.
typedef struct { int ant; char str[50]; }sample; main () { sample = { 1, "hi" }; char query[50]; scanf ("enter query object inside sample structure %s", query ); /* search , print function */ ex : query field ant how convert query string input "ant" a.ant , print value of ant ? }
please pardon me if not clear in explanation.
input : ant output : 1 input: str output : hi input: rat output: field not present
ask user member name , selectivity print contents.
lots of additional possibilities detect/ignore leading/trailing white-space, case insensitivity, eof handling, etc., given post's scant details.
char query[50]; puts("enter query object inside sample structure"); fgets(query, sizeof query, stdin); // trim potential \n query[strcspn(query, "\n")] = 0; if (strcmp(query, "ant")== 0) { printf("%d\n", a.amt); } else if (strcmp(query, "str")== 0) { printf("%s\n", a.str); } else { puts("field not present"); }
Comments
Post a Comment