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

查看:76
本文介绍了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);

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

rep =结束

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\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;
}

推荐答案

您的列表代码正确无误,当您使用浮点数的%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\n",broj);

当然,我强烈建议您注意另外两张照片,除非您想要的照片不准确.

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

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

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读取char时的警告中对此进行进一步讨论.(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天全站免登陆