Scanf() 取 0 而不是 Float 键盘输入 [英] Scanf() taking 0 instead of Float keyboard input

查看:16
本文介绍了Scanf() 取 0 而不是 Float 键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 scanf() 函数将 0 作为输入而不是通过键盘实际输入的问题.手头的任务非常简单.我需要使用函数 upis 将元素添加到链表,然后打印所有元素并清除列表.但是,我一直打印出 0.

I'm having problems with scanf() function taking 0 as input instead of the actual input via keyboard. The task at hand is pretty simple. I need to add elements to a linked list using function upis, and then print all the elements and clear the list. However, I kept getting 0s printed out.

起初我认为我的 upis 函数或我打印和清除列表的代码块有问题,但经过一些修补和添加 printf("BROJJJJJJJJJJJ:",broj);(打印我刚刚输入的数字),我意识到我一直在那里得到 0,所以上面一行中的 scanf() 肯定发生了一些事情.

At first I thought there was some problem with my upis function or with the block of code where I print and clear the list, but after some tinkering and adding printf("BROJJJJJJJJJJJ:",broj); (printing the number I just entered), I realised I kept getting 0s there, so there must be something going on with scanf() in the above line.

我尝试在 f 之前添加空格,但这根本没有帮助(scanf("%f", &broj);).

I tried adding blank space before f, but that didn't help at all (scanf("%f", &broj);).

由于部分代码不是英文,这里只是一个快速参考指南:

Since parts of code are not in English, here's just a quick reference guide:

cvor = 节点

novi = 新

glava = 根

sljed = 下一个

sljed = next

代表 = 结束

broj = 数字

#include <stdio.h>
#include <malloc.h>

typedef struct cvor {
    float element;
    struct cvor *sljed;
} cvor;

int upis(cvor **glava, cvor **rep, float *broj) {
    cvor *novi;
    novi=(cvor*)malloc(sizeof(cvor));
    if (novi == NULL) return 0;
    novi->element = *broj;
    novi->sljed = NULL;
    if (*glava == NULL) {
        *glava = novi;
        *rep = novi;
    }
    else {
        (*rep)->sljed = novi;
        *rep = novi;
    }
    printf("%d  ", (*glava)->element);
    return 1;
}

int main(void) {
    cvor *glava=NULL, *rep=NULL, *p=NULL;
    float broj;
    int n,i;

    do {
        printf("Unesite n<=10
");
        scanf("%d", &n);
    } while (n<=0 || n>10);

    /* upisivanje */
    for (i=0; i<n; i++) {
        printf("Unesite %d. clan liste
", i+1);
        **scanf("%f", &broj);**
        **printf("BROJJJJJJJJJJJ:",broj);**
        if (!(upis(&glava, &rep, &broj))) printf ("
Upis neuspjesan
");
        else printf ("
Upis uspjesan
");
    }

    /*ispisivanje i brisanje (od pocetka)*/
    for (;glava;) {
        printf("%d  ", glava->element);
        p = glava;
        glava = glava->sljed;
        free(p);
    }

    return 0;
}

推荐答案

你的列表代码是正确的,你用 %d 格式打印元素,而它们是浮点数,所以改变这个:

Your code of the list is correct, you print the elements with %d format, while they are floats, so change this:

printf("%d  ", (*glava)->element);

为此:

printf("%f  ", (*glava)->element);

并且应该看到正确的结果.例如

And should see the correct results. E.g.

./a.out 
Unesite n<=10
4
Unesite 1. clan liste
3.14
3.140000  
Upis uspjesan
Unesite 2. clan liste
2.78
3.140000  
Upis uspjesan
Unesite 3. clan liste
1.11
3.140000  
Upis uspjesan
Unesite 4. clan liste
2.22
3.140000  
Upis uspjesan
3.140000  2.780000  1.110000  2.220000  

<小时>

更具分析性的答案:

在启用一些警告的情况下编译,您应该得到:

Compile with some warnings enabled, and you should get:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
main.c:23:20: warning: format specifies type 'int' but the argument has type
      'float' [-Wformat]
    printf("%d  ", (*glava)->element);
            ~~     ^~~~~~~~~~~~~~~~~
            %f
main.c:41:34: warning: data argument not used by format string
      [-Wformat-extra-args]
        printf("BROJJJJJJJJJJJ:",broj);
               ~~~~~~~~~~~~~~~~~ ^
main.c:48:24: warning: format specifies type 'int' but the argument has type
      'float' [-Wformat]
        printf("%d  ", glava->element);
                ~~     ^~~~~~~~~~~~~~
                %f
3 warnings generated.

试试这个,而不是你的调试尝试:

Try this instead of your debugging attempts:

printf("BROJJJJJJJJJJJ: %f
",broj);

当然,我强烈建议您注意其他两个打印件,除非您想要不准确的打印件.

Of course, I strongly advice you to take care the other two prints, unless you want inaccurate prints.

没有malloc.h,为了使用malloc(和free),这样做:

There is no malloc.h, in order to use malloc (and free), do this:

 #include <stdlib.h>

不是什么导致了你的错误,而是我是否转换了 malloc 的结果? 没有.

Not what causing your errors, but Do I cast the result of malloc? No.

我尝试在...之前添加空格

I tried adding blank space before ...

这在处理字符时很有用.我在 使用 scanf 读取字符时的注意事项中进一步讨论了这一点(C).

This is useful when dealing with characters. I discuss this further in Caution when reading char with scanf (C).

这篇关于Scanf() 取 0 而不是 Float 键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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