简体   繁体   English

使用char和scanf以及for循环时遇到问题?

[英]Problems using char and scanf and the for loop?

My problem is using char and scanf. 我的问题是使用char和scanf。 Below you can find my code that is almost working right. 在下面,您可以找到几乎可以正常运行的代码。 The problem is concerning the for loop. 问题与for循环有关。 You see, I would like to ask 10 characters from the user, and count the alphabets, but for loop is not working properly. 您看,我想问用户10个字符,并计算字母,但是for循环无法正常工作。 It dosent count the inputs right. 它正确地计算输入。 For example, if the user types 6, it is fine, or a, it is also fine, but if he types for example 10 or 11, then the program thinks that the user typed 2 different character but when actually the user only typed one character "10". 例如,如果用户键入6,可以,或者a,也可以,但是如果他键入例如10或11,则程序会认为用户键入了2个不同的字符,但是实际上用户只键入了一个字符“ 10”。 So please, help me, and explain, why the scanf whit the char isnt working properly. 因此,请帮助我,并解释一下为什么charf无法正常工作。 Thank you! 谢谢!

#include <stdio.h>
#include <stdlib.h>


unsigned int countalpha(void){

printf("Please enter 10 characters:\n");

char b;
int sum = 0;
int i = 0;

for (i = 0; i<10; i++)
    {
    scanf("%c\n", &b);


    if (isalpha(b))
        sum = sum +1;
    else
        sum = sum;

    }


    return sum;

}

int main()
{
    int sum;

    sum = countalpha();
    printf ("Letters totally: %d", sum);

    return 0;
}

The scanf() function is incredibly hard to use correctly. scanf()函数难以正确使用。

Trailing white space in a scanf() format string is diabolical for interactive input. scanf()格式字符串中的scanf()空白对于交互式输入而言是同义的。 The notation "%c\\n" means 'read a character; 符号"%c\\n"表示“读取字符; then skip following white space until the next character that isn't white space, and only then return from the function'. 然后跳过后面的空格,直到下一个不是空格的字符,然后才从函数中返回。 This is seldom what you want. 这很少是您想要的。 The user will need to type a character, perhaps a , and a return, and another one, and some blanks, and so on, but the input won't return until, in frustration, they type 'you stupid program' (and a newline), at which point everything suddenly starts working, except that their comment probably wasn't the correct response for the next input. 用户将需要输入一个字符,也许是a ,然后再返回,再输入一个,再加上一些空白,依此类推,但是直到沮丧地输入“您愚蠢的程序”(和一个换行符),此时一切突然开始起作用,除了他们的注释可能不是对下一个输入的正确响应。 Or they hit interrupt and never run your program again because it is too frustrating for them. 否则他们会遇到中断,再也不会再次运行您的程序,因为这对他们来说太令人沮丧了。

Note that any white space character in a scanf() format string means 'skip optional white space', which means blanks, tabs, newline (and sundry more obscure characters) are skipped if they are present, but there's no problem if there is no white space. 请注意, scanf()格式字符串中的任何空格字符均表示“跳过可选空格”,这意味着如果存在空格,制表符,换行符(以及其他更晦涩的字符),则将它们跳过,但是如果没有,则没有问题空白空间。 Also note that the only conversion specifiers that don't skip white space anyway are %c , %[...] and %n ; 还要注意,无论如何,唯一不跳过空格的转换说明符是%c%[...]%n all other conversion specifiers automatically skip white space. 所有其他转换说明符会自动跳过空格。

You should probably be using " %c" . 您可能应该使用" %c" That means 'skip white space and then return the character that isn't white space'. 这意味着“跳过空白,然后返回非空白字符”。 Or, given your code, you can simply use "%c" , or even better just: 或者,根据您的代码,您可以简单地使用"%c" ,甚至更好:

int c;
while ((c = getchar()) != EOF)
{
    if (isalpha(c)
        sum++;
}

If you want line oriented input, do not use scanf() or fscanf() ; 如果要输入方向,请不要使用scanf()fscanf() use fgets() or getline() followed by sscanf() . 使用fgets()getline()然后使用sscanf()

Incidentally, the lines: 顺便说一下,这些行:

else
    sum = sum;

are pretty pointless. 毫无意义。 The compiler will probably optimize them away, but other people shouldn't be forced to read them and puzzle over what you were thinking. 编译器可能会优化它们,但是不应强迫其他人阅读它们并为您的想法感到困惑。

Is the user supposed to press ENTER between each character? 用户是否应该在每个字符之间按ENTER键? Your format string "%c\\n" indicates that they should, but your problem statement suggests otherwise. 您的格式字符串"%c\\n"表示应该这样做,但是您的问题说明中却相反。 Try removing the \\n . 尝试删除\\n

Further points: 其他要点:

  • scanf() returns the number of items parsed and assigned. scanf()返回已解析和分配的项目数。 You should always check this return to make sure that the expected number of items were assigned. 您应该始终检查此退货以确保分配了预期的项目数。
  • The following code is pointless and should be removed: 以下代码毫无意义,应将其删除:
    else
        sum = sum;

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

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