Avineshwar
Avineshwar

Reputation: 51

C:Function not returning correct value

The purpose of this program is to print as much input text as possible (up to the array limit) but it should explicitly return the exact length (numerical value) of the input text.

Below is my code:

#include <stdio.h>
#define MAXLINE 1000

int line_input(char array[], int maxlinelimit);
void copy(char toarray[], char fromarray[]);

int line_input(char s[], int limit)
{
    char ch;
    int i = 0;
    for(; ((ch = getchar()) != EOF) && (ch != '\n'); ++i)
    {
        s[i] = ch;
    }
    if(ch == '\n')
    {
        s[i] = ch;
        ++i;
    }
    s[i] = '\0';
    printf("%d\n", i);
    return i;
}

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

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

int main()
{
    int length;
    length = 0;
    int max;
    max = 0;
    char line[MAXLINE], longest[MAXLINE];
    while((length = line_input(line, MAXLINE) > 0))
    {
        printf("%d\n%s\n", length, line);
        if(length > max)
        {
            max = length;
            copy(longest, line);
        }

    }
    if(max > 0)
    {
        printf("\n\n%d is the size of the longest line\n", max);
        printf("%s\n\n", longest);
    }
    return 0;
}

You will find that the user-defined function named "line_input()" isn't returning the value it should due to some reason I do not know. I don't think there is any warning which I haven't tackled hopefully.

Please see if you can fix it.

P.S. - input text is any character string from the keyboard. Pressing "Enter" will start a new line though.

Upvotes: 0

Views: 118

Answers (2)

Chris Vasquez
Chris Vasquez

Reputation: 31

Look at your line_input() function:

int line_input(char s[], int limit)
{
    char ch;
    int i = 0;
    for(; ((ch = getchar()) != EOF) && (ch != '\n'); ++i)
    {
        s[i] = ch;
    }
    if(ch == '\n')
    {
        s[i] = ch;
        ++i;
    }
    s[i] = '\0';
    printf("%d\n", i);
    return i;
}

You are only incrementing your variable 'i' when 'ch' is a newline character. Put your increment in the for loop as follows:

for(; ((ch = getchar()) != EOF) && (ch != '\n'); ++i){
    s[i] = ch;
    i++;
}

You can decide if you want to keep the increment for newline characters or not by keeping or removing the increment from the if(ch == '\n') body if you choose to.

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

  • You have to use int, which can safely store all (not multi-byte) charcters and EOF, to store the result of getchar().
  • You must limit the length to save to avoid buffer overrun.
  • Stopped counting the newline character to make the return value more acccurate.

Try this:

int line_input(char s[], int limit)
{
    int ch;
    int i = 0, index = 0;
    for(; ((ch = getchar()) != EOF) && (ch != '\n'); ++i)
    {
        if (index < limit - 1) s[index++] = ch;
    }
    if(ch == '\n')
    {
        if (index < limit - 1) s[index++] = ch;
    }
    s[index] = '\0';
    printf("%d\n", i);
    return i;
}

Also note that the type of limit, i, index and the return value should be size_t because they are dealing with size.

UPDATE: another version which can tell blank line from end of input and with some more improvements

#include <stdio.h>
#define MAXLINE 1000

int line_input(char s[], int limit);
void copy(char to[], const char from[]);

int line_input(char s[], int limit)
{
    int ch;
    int i = 0, index = 0;
    for(; ((ch = getchar()) != EOF) && (ch != '\n'); ++i)
    {
        if (index < limit - 1) s[index++] = ch;
    }
    if(ch == '\n')
    {
        if (index < limit - 1) s[index++] = ch;
    }
    else if(ch == EOF && index == 0)
    {
        return -1;
    }
    s[index] = '\0';
    printf("%d\n", i);
    return i;
}

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

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

int main(void)
{
    int length = 0;
    int max = 0;
    char line[MAXLINE], longest[MAXLINE];
    while((length = line_input(line, MAXLINE)) >= 0)
    {
        printf("%d\n%s\n", length, line);
        if(length > max)
        {
            max = length;
            copy(longest, line);
        }

    }
    if(max > 0)
    {
        printf("\n\n%d is the size of the longest line\n", max);
        printf("%s\n\n", longest);
    }
    return 0;
}

Upvotes: 1

Related Questions