|
-
November 14th, 2007, 01:29 PM
#11
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.
-
November 14th, 2007, 01:40 PM
#12
Senior Member
Yes, I changed it. Here it is:
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;
}
I included <strdio.h>
If I do it in this way:
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;
}
it works. But still have problem with the print function
-
November 14th, 2007, 01:51 PM
#13
Hey how does the insert function work?
You have changed the argument to a single pointer. So it should be
Code:
currency* InsertItem(currency *list)
{
//.......
return list;
}
and in main() it should be called as
Code:
list=InsertItem(list);
[EDIT]
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);
}
Last edited by pi><boy; November 14th, 2007 at 02:46 PM.
-
November 14th, 2007, 02:18 PM
#14
Senior Member
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)
Last edited by Barov4e; November 14th, 2007 at 02:21 PM.
-
November 14th, 2007, 02:22 PM
#15
Hey see the above edited post ^^
-
November 14th, 2007, 03:06 PM
#16
Senior Member
pi><boy, you r the Man! Thanks a lot ... it is done!
Best regards!
-
November 14th, 2007, 03:14 PM
#17
you were segfaulting
-
November 14th, 2007, 04:31 PM
#18
Senior Member
argh ... another stupid exception. I want to add new items to the edn of the list and resource the function:
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;
}
And now it gives me exception on this row:
Hmmm ... obviously I have problems with these pointers
-
November 14th, 2007, 05:35 PM
#19
yeah you are having problems with pointers. I suggest you to do a bit more of reading up.
I think it would be
Code:
while(node->next!=NULL)
node=node->next;
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.
-
November 14th, 2007, 06:17 PM
#20
Senior Member
Nope, I tried it already and gives me exception on:
Code:
while(node->next!=NULL)
It don't allows me to change node->next. I can change it only before node=*list;
Similar Threads
-
By IcSilk in forum Operating Systems
Replies: 8
Last Post: October 30th, 2005, 11:01 PM
-
By XTC46 in forum Site Feedback/Questions/Suggestions
Replies: 15
Last Post: August 24th, 2005, 07:52 PM
-
By journy101 in forum Programming Security
Replies: 3
Last Post: August 13th, 2003, 05:31 PM
-
By pctwister in forum Hardware
Replies: 4
Last Post: July 24th, 2003, 08:27 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|