Hey guys and gals, I just started to use functions and I'm stuck on a question, the question is as follows,
Q. Based on program a (guess the word program),modify the program so that it uses a function to wait and read the user's input.
here is the code for a, this program works fine
Code:#include <string.h> #include <stdio.h> int main(void) { const char SECRET[]="pink"; char guess[20]; int x; for (x = 0; ((guess != "pink") && (x < 3)); x = x + 1) { printf("Guess the secret word\n"); scanf("%s",guess); printf("%s\n Is incorrect, please try again\n"); if(strcmp(SECRET,guess)==0) { printf("Correct guess - well done\n"); break; } } return 0; }
And here is my attempt
Code:#include <string.h> #include <stdio.h> void get_user_input(char*); int main(void) { const char SECRET[]="pink"; int x; get_user_input(&SECRET); return 0; } get_user_input(char* guess) { for (x = 0; ((guess != "pink") && (x < 3)); x = x + 1) { printf("Guess the secret word\n"); scanf("%s",guess); printf("%s\n Is incorrect, please try again\n"); if(strcmp(SECRET,guess)==0) { printf("Correct guess - well done\n"); break; } }
This doesn't work for me and I'm not really sure why, can anyone give me some pointers, thanks in advance



LinkBack URL
About LinkBacks



