Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换getchar();? #包括 内部主(空) { int n; printf(“给出您想要输入的字数。”); scanf(“%d”和“&n”); 整数字母[n],i,j,count,key,k; char-str[100]; //扫描每个单词,计算字母数并将其存储在下一个可用的 //“字母”数组中的位置。 对于(i=0;i_C_Loops_Infinite_Getchar - Fatal编程技术网

如何替换getchar();? #包括 内部主(空) { int n; printf(“给出您想要输入的字数。”); scanf(“%d”和“&n”); 整数字母[n],i,j,count,key,k; char-str[100]; //扫描每个单词,计算字母数并将其存储在下一个可用的 //“字母”数组中的位置。 对于(i=0;i

如何替换getchar();? #包括 内部主(空) { int n; printf(“给出您想要输入的字数。”); scanf(“%d”和“&n”); 整数字母[n],i,j,count,key,k; char-str[100]; //扫描每个单词,计算字母数并将其存储在下一个可用的 //“字母”数组中的位置。 对于(i=0;i,c,loops,infinite,getchar,C,Loops,Infinite,Getchar,在第一个循环中更新字母计数不是更容易吗 #include <stdio.h> int main (void) { int n; printf("Give the number of words you want to input."); scanf("%d",&n); int letters[n],i,j,count,key,k; char str[100]; //Scans each word, counts it's letters and stores it i

在第一个循环中更新字母计数不是更容易吗

#include <stdio.h>

int main (void)
{
int n;

printf("Give the number of words you want to input.");
scanf("%d",&n);

int letters[n],i,j,count,key,k;
char str[100];
 //Scans each word, counts it's letters and stores it in the next available 
 //position in "letters" array.
 for (i=0;i<n;i++)
    {
        j=0;
        printf("Give the next word.");
        do{
            str[j] = getchar();
            j++;
        }while (str[j-1]!='\n');
        str[j-1] = '\0';

        letters[i] = j;
    }

//Compacts the data by figuring out which cells have the same number of letters 
for (i=0;i<n;i++)
    {
        key = letters[i];
        count = 0;
        for (j=i+1;j<=n;j++)
            {
                if (key==letters[j])
                    {   
                        count += 1;
                        letters[j] = 0;
                    }
            }
        letters[i] = count;
    }

//creates a histogram
i=0;
do{
    printf("%d|",i);
    for (j=1;j<=letters[i];j++)
    {
        printf("*");
    }
    printf("\n");
    i++;
}while ((i<=n));

return 0;

}
memset(字母,0,n);

对于(i=0;i将代码的第一个块更改为如下所示:
(测试getchar的输出,仅当不是EOF时才继续)


以下两行具有错误的结束条件;应该是
您的代码正在直接保存为字符,这会丢失有关EOF的信息,而您并没有测试EOF。这是导致问题的原因。请记住:
getchar()
返回一个
int
,它要么是
char
(被视为无符号)的值,要么是表示EOF的负值(通常为-1)。这比
char
多了一个值,因此返回类型是
int
。我没有考虑其他错误。我相信这是错误的:s[-1]。除此之外,我真的不明白为什么它与我所做的不同,以及为什么它使第二个循环无效。*s='\0'可能是您的意思?(s[-1]具有负数组索引,这是不允许的)@Mechanic45:研究别人的代码,试图理解它是如何工作的以及为什么工作的,这将使你成为一个更好的程序员。仅仅相信它是错误的,不会。s将是换行符后的一个字节,据我所知,负索引是允许的。这没有帮助。我更改了它。然后运行了它。它得到了第一个正确的printf。然后我给了数字5。然后它打印了“下一个单词。下一个单词。”然后只接受了4个单词,而不是我期望的5个。在我输入第4个单词后,它开始到处打印“*”。我如何使用scanf()在这种情况下,既然我希望用户能够键入完整的单词?好的,我将使用printf和scanf组合编辑我的答案…(
scanf()
非常适合键入完整的单词)好的,我修改了你的建议,但不是“q”(假设“q”可以是用户输入的“有效”单词)例如,当用户点击esc时,我如何使其停止?检查EOF;查看关于和,当你是对的。关于直方图,它应该显示一个由n个字母组成的单词有多少次用星号(*)表示。字母数和直方图的行数之间没有关系。@Mechanic45:我说的是行号,不是行数。哦,但现在你提到了:行数也有问题。不过别担心,既然无限循环已经不存在了,你很快就会发现的。一句忠告:开始学习使用调试器,它将帮助你掌握你自己的程序。嗯……事实上,我的意思是:“……字母的数量和直方图的独立行的数量。”(不是行的总数)谢谢你的建议。
memset(letters, 0, n);
for (i=0;i<n;i++)
{
    char* s = str;
    int j=0;
    printf("Give the next word.");
    do{
        *s = getchar();
        ++j;
    }while (*(s++)!='\n');
    s[-1] = '\0';

    letters[j-1]++;
}
for (i=0;i<n;i++)
{
    j=0;
    printf("Give the next word.");
    do{
        a = getchar();
        if(a >= 0) 
        {
            str[j] = a;
            j++;
        }
        else break;
    }while (str[j-1]!='\n');
    str[j-1] = '\0';

    letters[i] = j;
}
int main(void)
{
    char buf[80]={""};
    while(  strcmp(buf, "q") != 0)  //enter a 'q' to quit
    {
        buf[0]=0;
        printf("enter string:\n");
        scanf("%s", buf);
        printf("%s\n", buf);
    }

}
for (j=i+1;j<=n;j++)

}while ((i<=n));
letters[i] = count;
letters[key] = count;