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.
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.
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 = 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;
}