0

I've been trying to use a function as a part of a code where the function analyses user input and finds the amount of suffixes used from a given list but the function enters an infinite loop and doesn't work properly - I can't figure out why. Edit: I've implemented your suggestions but it still sends me into a loop where it asks me to provide input and doesn't stop even if I put in '.'

bool read_sentence(char* noun_suffixes[], int noun_suffixes_len,
    char* verb_suffixes[], int verb_suffixes_len,
    char* adj_suffixes[], int adj_suffixes_len,
    int* num_of_nouns, int* num_of_verbs, int* num_of_adjs)
{
    char word[MAX_LEN]; char new_word[MAX_LEN];
    word[0] = '\0'; new_word[0] = '\0';
    int k = 0; unsigned int i = 0;
    while ((k == 0))
    {
        scanf("%s", word);
        printf("word is %s", word);
        for (i = 0; i < strlen(word) - 1; i++) {
            new_word[i] = toLower(word[i]);
            if (word[i] == '.')
            {
                k = 1;
            }
        }
        printf("%s", new_word);
        if (is_suffix_in_dict(word, noun_suffixes, noun_suffixes_len) == TRUE)
            *(num_of_nouns) += 1;
        if (is_suffix_in_dict(word, verb_suffixes, verb_suffixes_len) == TRUE)
            *(num_of_verbs) += 1;
        if (is_suffix_in_dict(word, adj_suffixes, adj_suffixes_len) == TRUE)
            *(num_of_adjs) += 1;
        word[0] = '\0'; new_word[0] = '\0';
    }
    return(TRUE);
}
kal_elk122
  • 143
  • 6
  • 3
    `scanf(("%s",word)!=EOF)` doesn't make any sense. Read the compiler warnings. – Lundin Jan 13 '21 at 09:30
  • Can you describe what's going on? Are repeatedly prompted for a word or is it looping elsewhere? – Tarik Jan 13 '21 at 09:32
  • @Lundin I think there is a way to type a EOF character at the prompt. Do not remember how though, maybe ctrl-z or something. – Tarik Jan 13 '21 at 09:33
  • @Lundin, As I thought: https://stackoverflow.com/questions/7373386/eof-in-windows-command-prompt-doesnt-terminate-input-stream – Tarik Jan 13 '21 at 09:34
  • Remove your second `word[0] = '\0';` – Paul Ogilvie Jan 13 '21 at 09:34
  • @Tarik It's Ctrl-D to send the EOF signal in the terminal – aureliar Jan 13 '21 at 09:34
  • Thank you! I tried removing it and placing the scanf earlier but I receive a segmentation error. specifically it directs me to the Line where new_word[i]=toLower(word[i]) – kal_elk122 Jan 13 '21 at 09:36
  • Tarik before removing the scanf(("%s",word)!=EOF) at Lundin's recommendation it just continued to ask for input without it, afterwards it says there's a segmentation error where new_word[i]=toLower(word[i]). – kal_elk122 Jan 13 '21 at 09:39
  • 2
    @aureliar ctrl-D on Linux, ctrl-Z on Windows. – Tarik Jan 13 '21 at 09:39
  • `printf("%s", new_word);`: you must still put a `\0` at the end of `new_word`. – Paul Ogilvie Jan 13 '21 at 09:39
  • "you must still put a \0 at the end of new_word" - how? – kal_elk122 Jan 13 '21 at 09:42
  • `new_word[i]= '\0';` after the loop (before the `printf`). – Paul Ogilvie Jan 13 '21 at 09:42
  • `while(scanf(("%s",word)!=EOF)&&(k==0))` will read a word before testing `k==0`. You should reverse this. – Paul Ogilvie Jan 13 '21 at 09:44
  • You can just `break` the loop at `k=1;`. I don't think you want anything after the `.` (So you don't even need `k`) – Paul Ogilvie Jan 13 '21 at 09:47
  • 2
    To all confused people, `scanf(("%s",word)!=EOF)` will not do anything sane at all. `"%s"` is evaulated as void expression being the left operand of the comma operator. Then `word` is evaluated as a lvalue, right operand of comma operator. It is probably some pointer pointing at la-la-land. Then that pointer is compared with `!=EOF`, which is invalid use of the != operator since EOF boils down to an integer. And the result of all that trash is then passed as a parameter to scanf. – Lundin Jan 13 '21 at 10:00
  • @Tarik Please note that the problem pointed out by Lundin is the position of the brackets, not the EOF. – Bob__ Jan 13 '21 at 10:07
  • I'm not sure I understand why the problem is in the position of the brackets - how are they supposed to look like? also why is the revised version of the program in an infinite input collecting loop as well? (keeps asking for input and doesn't print anything) – kal_elk122 Jan 13 '21 at 10:12
  • @Bob_ Well, as long as the user does not type ctrl-Z he stays in the loop too. Now, there seem to be multiple problems that the OP should learn to solve using the debugger. – Tarik Jan 13 '21 at 10:19
  • Clarification - the problem was with spacing in the main function - it screwed with scanf. after removing it the program worked properly - thank you all!! I 'm still interested as to the meaning of the why the problem is in the position of the bracket regarding scanf(("%s",word)!=EOF). – kal_elk122 Jan 13 '21 at 10:33
  • 1
    `scanf( ("%s",word) != EOF )` --> `scanf("%s", word) != EOF` You have to compare the value *returned* by scanf to EOF, not *pass* a meaningless integer value to scanf. Ideally avoid scanf at all and read a whole line with fgets and then sscanf each word. – Bob__ Jan 13 '21 at 10:38
  • I see, thank you! unfortunately my school doesn't really teach us anything but scanf, but I'll look the other function up - thank you – kal_elk122 Jan 13 '21 at 11:12

0 Answers0