利用用户输入scanf()的错误 [英] Error using scanf() for user input

查看:109
本文介绍了利用用户输入scanf()的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 scanf()的将数据存储到一个联盟。

得到一个奇怪的问题

下面是我的code

 的#include<&stdio.h中GT;工会学生
{
    浮动分数;
    焦炭等级;
};INT主要(无效)
{
    工会学生插孔;    的printf(请输入学生分数:);
    scanf函数(%F,&安培; jack.score);
    的printf(分数:%F,jack.score);    jack.score = 0;    的printf(请输入学生成绩:);
    scanf函数(%C,&安培; jack.grade);
    的printf(等级:%C,jack.grade);}

我得到以下输出

  searock @ searock桌面:〜/桌面$ ./union
输入学生分数:12
得分:12.000000Enter学生等级:等级:

但如果我改变我的code为:

 的#include<&stdio.h中GT;工会学生
{
    浮动分数;
    焦炭等级;
};INT主要(无效)
{
    工会学生插孔;    的printf(请输入学生成绩:);
    scanf函数(%C,&安培; jack.grade);
    的printf(等级:%C \\ N,jack.grade);    的printf(请输入学生分数:);
    scanf函数(%F,&安培; jack.score);
    的printf(分数:%F \\ N,jack.score);}

它给我确切的输出[正确的输出。我知道这是不是一个很好的例子,但也有人解释我是怎么回事?

修改code:添加之前\\ n格式字符串。 [scanf函数(\\ N%C,&安培; CH)]

 的#include<&stdio.h中GT;    工会学生
    {
        浮动分数;
        焦炭等级;
    };    INT主要(无效)
    {
        工会学生插孔;        的printf(请输入学生分数:);
        scanf函数(%F,&安培; jack.score);
        的printf(分数:%F,jack.score);        jack.score = 0;        的printf(请输入学生成绩:);
        scanf函数(\\ N%C,&安培; jack.grade);
        的printf(等级:%C,jack.grade);    }


解决方案

在第一个例子中,第一个 scanf()的读了一些,但不包括换行符(假设不键入空格只是和换行;如果你这样做,它会继续等待输入直到你提供一个数字或一个非数字 - 在空格等不能算作一个数字或一个非数字)。然后第二个 scanf()的%C 格式说明不跳过空白(不同于其他格式符),并读取行(假设你键入的换行符号结束后立即,如果你输入别的东西 - 空格或字母,也许,它会读取该字符),完全无视任何你在第二行中键入。 (事实上​​,当我运行code,它不会等我进入了第二批输入的任何东西。试着输入3.14 + C或3.14C和一个换行符。)

在第二个例子中,第一个 scanf()的读取第一个字符。第二个 scanf()的跳过空格,包括换行,直到它找到一个数字或东西是明确不是数字(如字母)。

这就是为什么大多数人避免一个美丽的示范 scanf()的;它是非常困难的在它提供满意的控制。你会更好看行(可能使用与fgets();绝对不使用获得()),然后解析它们与的sscanf()。然后你会得到理智的行为两个例子。

请注意,你的问题是完全无关的使用联盟的;您code是罚款的工作方式。你的问题是所有的捆绑与使用 scanf()的

小建议:在打印输出的线路时 - 除提示 - 包括格式字符串的结尾换行

I am getting a weird problem while using scanf() to store data into a union.

Here's my code

#include <stdio.h>

union Student
{
    float score;
    char grade;
};

int main(void)
{
    union Student jack;

    printf("Enter student score : ");
    scanf("%f", &jack.score);
    printf("Score : %f", jack.score);

    jack.score=0;

    printf("Enter student grade : ");
    scanf("%c", &jack.grade);
    printf("Grade : %c", jack.grade);

}

I get the following output

searock@searock-desktop:~/Desktop$ ./union
Enter student score : 12
Score : 12.000000Enter student grade : Grade :

but if I change my code to:

#include <stdio.h>

union Student
{
    float score;
    char grade;
};

int main(void)
{
    union Student jack;

    printf("Enter student grade : ");
    scanf("%c", &jack.grade);
    printf("Grade : %c\n", jack.grade);

    printf("Enter student score : ");
    scanf("%f", &jack.score);
    printf("Score : %f\n", jack.score);

}

It gives me the exact output [correct output]. I know this is not a good example, but can someone explain me what's going wrong?

Modified Code : Add \n before format string. [scanf("\n%c", &ch);]

#include <stdio.h>

    union Student
    {
        float score;
        char grade;
    };

    int main(void)
    {
        union Student jack;

        printf("Enter student score : ");
        scanf("%f", &jack.score);
        printf("Score : %f", jack.score);

        jack.score=0;

        printf("Enter student grade : ");
        scanf("\n%c", &jack.grade);
        printf("Grade : %c", jack.grade);

    }

解决方案

In the first example, the first scanf() reads a number up to but not including the newline (assuming you don't type just blanks and newlines; if you do, it will continue waiting for input until you provide a number or a non-number - where blanks etc do not count as either a number or a non-number). Then the second scanf() with the '%c' format specifier does not skip white space (unlike most other format specifiers) and reads the newline (assuming you typed a newline immediately after the end of the number; if you typed something else - white space or a letter, perhaps, it reads that character), completely ignoring anything you typed on the second line. (Indeed, when I run the code, it does not wait for me to enter anything for the second lot of input. Try typing "3.14+C" or "3.14C" and a newline.)

In the second example, the first scanf() reads the first character. The second scanf() skips over white space, including newlines, until it finds either a number or something that is definitively not a number (like a letter).

This is a beautiful demonstration of why most people avoid scanf(); it is very difficult to provide satisfactory control over it. You would be better off reading lines (perhaps using fgets(); definitely not using gets()) and then parsing them with sscanf(). You would then get sane behaviour for both examples.

Note that your problem is completely unrelated to the use of a union; your code is fine in the way that is working. Your problem is all tied up with the use of scanf().

Minor recommendation: when printing lines of output - other than prompts - include a newline at the end of the format string.

这篇关于利用用户输入scanf()的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆