-1

I have this function printLongestLine which I want to print the length of every line of input as well as the greatest length. For example, if the input was:

Hey
I love stack

The output should be:

The length of line 1 is 3
The length of line 2 is 15
The greatest line length is 15

But the output actually is:

The length of line 1 is 3
The length of line 2 is 15
The length of line 3 is -858993460
The length of line 4 is -858993460
The length of line 5 is -858993460
The length of line 6 is -858993460
The length of line 7 is -858993460
The length of line 8 is -858993460
The length of line 9 is -858993460
The length of line 10 is -858993460
The length of line 11 is -858993460
The length of line 12 is -858993460
The length of line 13 is -858993460
The length of line 14 is -858993460
The length of line 15 is -858993460
The length of line 16 is -858993460
The length of line 17 is -858993460
The length of line 18 is -858993460
The length of line 19 is -858993460
The length of line 20 is -858993460
The greatest line length is 15

I have tried to fix the for loop but do not see the error traversing my lineLength array to no avail.

#define NEWLINE 1
#define MAXLINES 20 //max number of lines
#define IN 0

//Precondition - Lines that are only /n will not be printed.
//Postcondition - prints the lengths of all the lines, and then the max line length of the input
printLongestLine()
{
    int lineLength[MAXLINES];
    int currentLine = 0;
    int status = NEWLINE;
    int currentLength = 0;
    int c;
    while ((c = getchar()) != EOF)
    {

        if ((c == '\n'))   
        {
            status = NEWLINE;
            lineLength[currentLine] = currentLength;
            currentLine++;
        }
        else if ((status == NEWLINE))
        {
            status = IN;
            currentLength++;

        }
        else
        {
            status = IN;
            currentLength++;
        }
    }
    int maxLength = 0;
    for (int i = 0; i < MAXLINES; i++)
    {
        if (lineLength[i] > maxLength)
        {
            maxLength = lineLength[i];
            printf("The length of line %d is %d\n", (i + 1), lineLength[i]);
        }
        else if ((lineLength[i] == 0) || lineLength[i] == NULL)
            continue;
        else
            printf("The length of line %d is %d\n", (i + 1), lineLength[i]);
    }
    printf("The greatest line length is %d", maxLength);
}   
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Smells like uninitialized variables. You don't check if you go past `MAXLINES`, do you? You also don't keep track of how many lines you actually read. It's also not clear what the double-brackets are for, like `if ((c == '\n'))` should actually be `if (c == 0x0A)`. This code has more and more issues the deeper I look, so you probably need to break up this problem into several smaller ones. I don't see the need for an array at all, just a current line length and a max variable. – tadman Jun 04 '19 at 00:46
  • 1
    You should set `currentLength` to `0` when you read a newline. – Barmar Jun 04 '19 at 00:47
  • I don't see the reason for the `status` variable. When `c` is not a newline, you do the same thing if `status == NEWLINE` and `else`. – Barmar Jun 04 '19 at 00:48
  • The second line is only 12 characters, but you say the expected output is 15. – Barmar Jun 04 '19 at 00:50
  • Two problems: (1) after seeing a newline, you need to reset `currentLength` to 0, and (2) only look at the `lineLength` entries that you've actually set (currently you're reading past that, all the way to `MAXLINES`, and picking up uninitialized values). – Tom Karzes Jun 04 '19 at 01:08
  • -858993460 = 0xCCCCCCCC => [uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Jun 04 '19 at 03:12

1 Answers1

2

You should only iterate as far as you've counted:

for (int i = 0; i < currentLine; i++) {
  // ...
}
tadman
  • 208,517
  • 23
  • 234
  • 262