the code I posted was a c++ code, did you first change it into a standardized c code? It seems it can't find the header file for gets().
#include<stdio.h> should be enough. Dunno about what's wrong now.
Printable View
the code I posted was a c++ code, did you first change it into a standardized c code? It seems it can't find the header file for gets().
#include<stdio.h> should be enough. Dunno about what's wrong now.
Yes, I changed it. Here it is:
I included <strdio.h>Code:void InsertItem(currency **list)
{
currency *temp;
fflush(stdin);
printf("Vavedi valuta: ");
gets(temp->name);
printf("Vavedi kod na valutata: ");
gets(temp->code);
printf("Vavedi stoinost za 1USD: ");
scanf("%f", &temp->value);
temp->number=++cnt;
temp->next=*list;
*list=temp;
}
If I do it in this way:
it works. But still have problem with the print functionCode:void InsertItem(currency *list)
{
currency temp;
fflush(stdin);
printf("Vavedi valuta: ");
gets(temp.name);
printf("Vavedi kod na valutata: ");
gets(temp.code);
printf("Vavedi stoinost za 1USD: ");
scanf("%f", &temp.value);
temp.number=++cnt;
temp.next=list;
list=&temp;
}
Hey how does the insert function work?
You have changed the argument to a single pointer. So it should be
and in main() it should be called asCode:currency* InsertItem(currency *list)
{
//.......
return list;
}
[EDIT]Code:list=InsertItem(list);
here is the whole code after I made some changes. It works totally fine for me except for the fact that it can't do scanf("%f",&temp->value). It shows some "floating point formats not linked" on the borland compiler. But I think you won't be seeing that on something like cc or gcc.
Code:#include <stdio.h>
typedef struct curr
{
char name[50];
char code[3];
float value;
int number;
struct curr *next;
}currency;
currency *start_ptr = NULL;
int cnt = 0;
void Print(currency *list)
{
currency *temp=list;
while(temp!=NULL)
{
printf("%d",temp->number);
temp=temp->next;
}
}
void InsertItem(currency **list)
{
currency *temp;
temp=(currency*)malloc(sizeof(curr));
fflush(stdin);
printf("Vavedi valuta: ");
gets(temp->name);
printf("Vavedi kod na valutata: ");
gets(temp->code);
temp->number=++cnt;
temp->next=*list;
*list=temp;
}
void main()
{
currency *list = NULL;
InsertItem(&list);
Print(list);
}
Ok, but now I get back to the previous problem - the cycle in the print function loop forever. And another thing - if I call Print(), without called InserItem before, it don't show me "Spisakat e prazen", but gives me an exception on:
Code:while(temp->next!=NULL)
Hey see the above edited post ^^
pi><boy, you r the Man! Thanks a lot ... it is done!
Best regards!
you were segfaulting ;)
argh ... another stupid exception. I want to add new items to the edn of the list and resource the function:
And now it gives me exception on this row:Code:void InsertItem(currency **list)
{
currency *temp, *node;
temp=(currency*)malloc(sizeof(curr));
node=*list;
while(node)
node=node->next;
fflush(stdin);
printf("Vavedi valuta: ");
gets(temp->name);
printf("Vavedi kod na valutata: ");
gets(temp->code);
printf("Vavedi stoinost za 1USD: ");
scanf("%f", &temp->value);
temp->number=++cnt;
temp->next=NULL;
node->next=temp;
*list=node;
}
Hmmm ... obviously I have problems with these pointers :(Code:node->next=temp;
yeah you are having problems with pointers. I suggest you to do a bit more of reading up.
I think it would be
Your code is while(node). That wouldn't take the pointer node to the last node in the list. Rather it would take you to the link of the last node i.e. NULL and only then will the while loop stop working.Code:while(node->next!=NULL)
node=node->next;
Nope, I tried it already and gives me exception on:
It don't allows me to change node->next. I can change it only before node=*list;Code:while(node->next!=NULL)