Hello, new here and hoped someone could assist me.
The description for the task was:
returns 1 if a word is successfully read & stored in the array word of n characters; otherwise returns 0 on end of file or when the user inputs the word eof
I think I am close but it doesn't quite work so I was hoping someone could point out what's going wrong, because I am not seeing it!
The problems I am having are:
- when I try to enter the eof (byebyenow in this case) the program tells me it is too long, and doesn't exit.
- a word with <= 4 characters will cause a "STATUS_ACCESS_VIOLATION" error and it creates a stackdumpfile
- 5 or 6 characters is also apparently too long
Thanks for helping!
Code:#include <stdio.h> #include <string.h> #define BUFSIZE 32 int get_word(const char prompt[], char word[], size_t n, const char eof[]); int get_word(const char prompt[], char word[], size_t n, const char eof[]) { char line[BUFSIZE]; while(1){ printf("%s\n", prompt); if(fgets(line, BUFSIZE, stdin) != NULL) { if(strcmp(line, eof) == 0) { printf("Program over."); return 0; } if(strlen(line) < n) { strcpy(word, line); printf("%s was 6 or less chars. Thankyou\n", line); return 1; }else{ printf("%s was too long! Try again.\n", line); } } } } int main(void) { char input[6]; if(get_word("Enter a word with 6 chars or less, or BYEBYENOW to quit.", input[6], 6, "BYEBYENOW") == 1) { printf("%s\n", input); } return 0; }



LinkBack URL
About LinkBacks





. It's pretty crappy.