简体   繁体   English

C:函数未返回正确值

[英]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. 您会发现由于某些我不知道的原因,名为“ line_input()”的用户定义函数未返回其应有的值。 I don't think there is any warning which I haven't tackled hopefully. 我认为没有希望解决的任何警告。

Please see if you can fix it. 请查看是否可以修复它。

PS - input text is any character string from the keyboard. PS- 输入文本是键盘上的任何字符串。 Pressing "Enter" will start a new line though. 按“ Enter”将开始新的一行。

  • You have to use int , which can safely store all (not multi-byte) charcters and EOF , to store the result of getchar() . 您必须使用int来存储getchar()的结果,该int可以安全地存储所有(非多字节)字符和EOF
  • 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. 另请注意, limitiindex和返回值的类型应为size_t因为它们正在处理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;
}

Look at your line_input() function: 查看您的line_input()函数:

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. 仅当'ch'是换行符时,才递增变量'i'。 Put your increment in the for loop as follows: 将您的增量放在for循环中,如下所示:

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. 如果选择保留或删除if(ch == '\\n')正文中的增量, if(ch == '\\n')可以决定是否要保留换行符的增量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM