-5

I've just started to read K&R and on pages 32-33, there is a code that

finds the longest line among the inputs.

I nearly completely copy-pasted the code given in the book, just added some comment lines to make the code more understandable for me. But it isn't working.

Edit: I'm sorry for bad questioning. It seems the program does not act properly when I press Ctrl + Z, in order to terminate it. No matter how many lines I type and how many times I press Ctrl + Z, it just does nothing.

The following is my version of the code:

/* Find the longest line among the giving inputs and print it */

#include <stdio.h>
#define MAXLINE 1000            /* maximum input line length */

int getLine(char line[], int maxLine);
void copy(char to[], char from[]);

int main(void) {
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here*/

    max = 0;

    /* getLine function takes all the input from user, returns it's size and equates it to the variable len
     * Then, len is compared whether it's greater than zero because if there's no input, no need to do any calculation
     * EDGE CASE
     */
    while ((len = getLine(line, MAXLINE)) > 0)
        /* If the length of input is larger than the previous max length, set max as the new length value and copy that input */
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) /* there was a line, EDGE CASE */
        printf("%s", longest);

    return 0;
}

/* Read a line into s, return length.
 * Since the input length is unknown, there should be a limit */
int getLine(char s[], int lim) {
    int c, i;

    /* The loop's first condition is whether the input length is below the limit. EDGE CASE
     * If it's not, omit the rest because it would cause a BUFFER OVERFLOW. Next, take the input as long as it's not an EOF command.
     * Finally, if the input is end of line, finish the loop, don' take it.
     */
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
        s[i] = c;
    if (c == '\n')
        s[i++] = c;
    s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string.
    return i;
}

void copy(char to[], char from[]) {
    int i = 0;

    // This loop is readily assigns all chars from the source array to the target array until it reaches the ending char.
    while ((to[i] = from[i]) != '\0')
        ++i;
}

Thanks in advance!

Said
  • 187
  • 1
  • 2
  • 11
  • What's the question? – acraig5075 Dec 07 '17 at 10:23
  • @acraig5075 Problem is "find the longest line among the lines typed by user". My problem is I nearly copy-pasted the same code given in the book with just a few comments but it isn't working. – Said Dec 07 '17 at 10:25
  • 1
    If someone came to you with a problem, and just said "But it somehow isn't working", what would you do? :/ – unwind Dec 07 '17 at 10:25
  • 1
    *How* isn't it working? For some example input, what is the expected and actual output? Perhaps you should take some time to [learn how to debug programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? Also, http://idownvotedbecau.se/itsnotworking/ – Some programmer dude Dec 07 '17 at 10:25
  • @unwind I'd advise him/her to debug the code but I'm using Eclipse and couldn't figure out how to debug my code yet. – Said Dec 07 '17 at 10:26
  • @SaidBuyukarslan So you wouldn't try to understand the problem by asking *what happens?" – unwind Dec 07 '17 at 10:29
  • @unwind, The problem is code isn't catching the EOF sign when I press Ctrl + Z. I should've mentioned it on the first hand, I got it, sorry. Edited the original post, as well. – Said Dec 07 '17 at 10:33
  • May not be related, but `getLine` is wrong. If you enter an empty line by just pressing the Enter key, the functions returns 2. Also the trailing `'\n'` is left at the end of the buffer, not sure if this is intentional. – Jabberwocky Dec 07 '17 at 10:34
  • CTRL+Z usually stops the process, but doesn't terminates it. CTRL+D would be better to terminate the input flow. – Jean-Baptiste Yunès Dec 07 '17 at 11:23

1 Answers1

3

Okay, here's the error:

s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string.

This will cause it to terminate the string even for no input (when it got EOF directly), and since it increments i before returning it, getLine() will never return 0 and thus main() will never stop. Removing the ++ fixed it, for my simple test.

Also, the comment is misleading, the compiler doesn't know anything. The compiler is no longer around when the code runs; the in-memory format of strings is something that's needed to keep the run-time libraries happy since that's what they expect.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • So, that means I managed to create a bug that is not even coming up on standart conditions :D I got your point. Now the code works fine. Thank you for your patience and guidence. – Said Dec 07 '17 at 10:46