Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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/3/arrays/14.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在c中仅保存1个字符_C_Arrays_While Loop_User Input_Getchar - Fatal编程技术网

Getchar在c中仅保存1个字符

Getchar在c中仅保存1个字符,c,arrays,while-loop,user-input,getchar,C,Arrays,While Loop,User Input,Getchar,所以,我要提前回答,这可能会让我的问题被记下来,但我没有太多的东西要说,所以这可能是一个不完整的问题,但我完全被卡住了。我正在用C编写一个程序,以便能够计算用户提供的字符的出现次数。目前,我正在尝试在while循环中使用getchar()保存字符,因为我尝试显示结果,它会在循环中为我提供整个输入,但当它退出循环时,它会保存最后一个字符(我理解它为什么这样做,而不是如何修复它)。注意:此代码包含回答此问题不需要的额外部分,这些部分将在我的代码后面提供: 这是我目前的代码(3/12/18),很抱歉,

所以,我要提前回答,这可能会让我的问题被记下来,但我没有太多的东西要说,所以这可能是一个不完整的问题,但我完全被卡住了。我正在用C编写一个程序,以便能够计算用户提供的字符的出现次数。目前,我正在尝试在while循环中使用getchar()保存字符,因为我尝试显示结果,它会在循环中为我提供整个输入,但当它退出循环时,它会保存最后一个字符(我理解它为什么这样做,而不是如何修复它)。注意:此代码包含回答此问题不需要的额外部分,这些部分将在我的代码后面提供: 这是我目前的代码(3/12/18),很抱歉,我不能再详细了:

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#define N 26

int main(void) {

    char ch, index;
    int count[N] = {0}, tolower(int ch);

    printf("Enter a sentence (end by '.'): \n");
    while(ch != '.') {
        ch = getchar();
        putchar(ch);  \\print testing to see where the characters are "leaving"
    }
    printf("%c", ch);  \\print testing to see if the characters go past the loop

}
#包括
#包括
#包括
#定义N 26
内部主(空){
char-ch,索引;
int count[N]={0},tolower(int ch);
printf(“输入一个句子(以“.”结尾):\n”);
而(ch!='。){
ch=getchar();
putchar(ch);\打印测试以查看字符“离开”的位置
}
printf(“%c”,ch);\\print测试字符是否通过循环
}

您需要按以下顺序执行循环事件:

  • 读一个字
  • 如果循环是
    ”。
    EOF
  • 写这个角色
  • 转到1
  • 另外,您需要使用
    int
    来存储
    getchar()
    的返回值,因为否则您无法检查
    EOF
    (它不是字符)

    如果您将这些步骤翻译成代码,您将得到:

    int ch;
    for (;;)
    {
         ch = getchar();
         if ( ch == '.' || ch == EOF )
              break;
         putchar(ch);
    }
    
    尽管将前两条语句组合到循环条件中是一种常见的习惯用法:

    int ch;
    while ( (ch = getchar()) != '.' && ch != EOF )
        putchar(ch);
    

    (1) getchar()不“保存”任何内容。它从输入中获取下一个字符,仅此而已。如果你想把这些保存在某个地方,那是你的工作。(2) getchar()返回一个int,而不是char,因此您需要为它使用一个int变量。(3)
    tolower()
    应该通过包含
    来定义,而不是通过
    main()
    中的(奇怪的)内联声明来定义(1)Ahhhhhh,这就解释了这一点,我假设getchar在保存char时像scanf一样(2)感谢您的输入,我将确保修复(3)我认为“tolower()”是通过字符串库给出的,谢谢您的输入。(4) 我需要能够输入一个在遇到“.”时结束的句子,该句子被保存并通过一个for循环(尚未写入)运行,该循环检查用户给出的每个字母的出现情况。
    scanf
    不会保存字符。我知道,我想它会像scanf一样保存,但为了char,它解决了我的问题,当我为我的程序的下一部分编写for循环时,它保存了整个字符串似乎,我感谢您的帮助:)