Or get this: A keyboard with an LCD for each key
Printable View
Or get this: A keyboard with an LCD for each key
Very cool!Quote:
Or get this: A keyboard with an LCD for each key
I have troubles with my function. I want to put a name in list, but, if the name is already there then let it prints that name already exists, if no, put name into list. Here is mine function...Code:struct student{
char ime[20];
struct student *next;
};
struct student *head=NULL;
When I type the name that already is in the list, it prints nothing, when i print the list, that name is just bunch of symbols. So it puts something inside but....Code:struct student *dodajAN(struct student *head){
struct student *novi;
novi = (struct student *)malloc(sizeof(struct student));
struct student *temp;
temp = head;
while(temp!=NULL){
if(novi->ime==temp->ime){
printf("Vec ima...");
}
temp=temp->next;
}
novi->next=head;
head=novi;
}
I know that I need to have a pointer on the list that has names inside, head is a pointer to first element of the list so i m missing that other pointer to list with names? help :confused:
To compare two strings, use strcmp() from <string.h>. http://www.cplusplus.com/reference/c...ng/strcmp.html
Don't use ==.
no, same thing. It has to do something with a pointer on a fulfilled list.
You see that? It will always be false, no matter what is contained in the imes, unless novi and temp both point to the same structure, in which case it will always be true. You might as well writeCode:if(novi->ime==temp->ime){
If your intent was to compare the strings for equality, useCode:if(novi == temp) {
Code:if(strcmp(novi->ime, temp->ime) == 0) {
No, it does not work! I try to compare strings but nothing. It puts something inside the list anyway! (symbols mess)!
Code:struct student *dodajAN(struct student *head){
struct student *novi;
novi = (struct student *)malloc(sizeof(struct student));
struct student *temp;
temp = head;
while(temp!=NULL){
if(strcmp(novi->ime,temp->ime)==0)
printf("Vec ima ime!");
return;
temp=temp->next;
}
novi->next=head;
head=novi;
return novi;
}
Program crashes after all with some nasty warning 'could not write in memory 00000000x6.....'...Code:case 5:
printf("Upisi ime: ");
scanf("%s", &ime);
head = dodajAN(head);
If ime is a char[] or a char*, drop the &.Code:scanf("%s", &ime);
You're not returning anything, whilst the function is supposed to return a structure . . .Code:if(strcmp(novi->ime,temp->ime)==0)
printf("Vec ima ime!");
return;
temp=temp->next;
}
shardinQuote:
You're not returning anything, whilst the function is supposed to return a structure . . .
Sometimes so stupid... - i think that says it all!!!
Everyone makes mistakes. :) Besides, your compiler should tell you about that particular mistake. Perhaps you should enable warnings. In Dev-C++, add -W -Wall to the "Extra compiler options" textbox in the Compiler Options dialog.
I have to disappoint you Dwks, now when i return "head", it doesn't break but it's not working also. It wont print my text, and name i enter is messed up. I want to make it to exit function if my condition is true. If not then add new name and exit function. I tried whith "return head"...Also if I type the new name that is not in the list it wont add it.
Returning head from here is probably not the best idea:
I think you should return novi or temp (either would probably work, since their imes are the same).Code:struct student *dodajAN(struct student *head){
struct student *novi;
novi = (struct student *)malloc(sizeof(struct student));
struct student *temp;
temp = head;
while(temp!=NULL){
if(strcmp(novi->ime,temp->ime)==0)
printf("Vec ima ime!");
return;
temp=temp->next;
}
novi->next=head;
head=novi;
return novi;
}
Actually, now that I look at your code again, it seems that the contents of novi are not initialized. So comparing it with temp is probably a bad idea as well.
Finally, I see something else. Your indentation may have caused you to miss this, so here is your while loop re-indented:
In other words, the return statement always executes on the first iteration of the while loop. You probably meant to use curly braces around the if statement to include the if statement in there:Code:while(temp!=NULL){
if(strcmp(novi->ime,temp->ime)==0)
printf("Vec ima ime!");
return;
temp=temp->next;
}
Also, I'm sure this has already been mentioned, but you shouldn't cast malloc().Code:while(temp!=NULL){
if(strcmp(novi->ime,temp->ime)==0) {
printf("Vec ima ime!");
return;
}
temp=temp->next;
}