Scanf()取0而不是Float键盘输入

时间:2017-05-29 20:42:35

标签: c io linked-list floating-point scanf

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

起初我以为我的upis功能或我打印并清除列表的代码块有问题,但经过一些修补和添加 printf(“BROJJJJJJJJJJJ:”,broj); (打印我刚刚输入的数字),我意识到我一直在那里得0,所以上面一行中必须有scanf()。

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

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

cvor = node

novi = new

glava = root

sljed = next

rep = end

broj = number

#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\n");
        scanf("%d", &n);
    } while (n<=0 || n>10);

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

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

    return 0;
}

2 个答案:

答案 0 :(得分:2)

三个问题:

  1. 未测试scanf的返回值始终是错误
  2. 您不会扫描输入中的换行符或空格,因此进一步的scanf调用可能会返回0(您没有进行测试;空格不能成为其中一部分浮动)并保持关联的参数不变。使用类似这样的东西,在浮动之前跳过任意数量的空白字符:

    if (scanf (" %f", &broj) == 1) { /* got something */ } else { /* EOF or error */ }

  3. 格式不匹配int与float。

答案 1 :(得分:0)

您的列表代码是正确的,您使用%d格式打印元素,而它们是浮点数,所以更改此内容:

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

到此:

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

应该看到正确的结果。 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  

更多分析答案:

编译时启用了一些警告,你应该得到:

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.

尝试此操作而不是调试尝试:

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

当然,我强烈建议你注意其他两张照片,除非你想要不准确的照片。

没有malloc.h,为了使用malloc(和free),请执行以下操作:

 #include <stdlib.h>

不是导致错误的原因,而是Do I cast the result of malloc?否。

  

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

这在处理字符时很有用。我在Caution when reading char with scanf (C)进一步讨论了这个问题。