help with C

N

Nick Keighley

sulays said:
I need to insert and sort in a link list

how far have you got so far with your problem? Could you show us the
code you
have written so far or explain what you have are having difficulties
with?
 
S

sulays

in getting an infinite loop dont know why?

this is in main
while(!feof(fpA))
{
readingaemployeedata(&fpA,lastn,firstn,midini,&empid,title,balance);
if(feof(fpA))
break;
tempPtr =
creatingaemployeedata(lastn,firstn,midini,empid,title,balance);
while(tempPtr != NULL)
{
prinheader();
firstemployeePtr = inserting(firstemployeePtr,tempPtr);
printing(firstemployeePtr);
}

function

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)
return tempPtr;

curr = prev = fPtr;

while(curr != NULL)
{
if(strcmp(tempPtr->lastname,curr->lastname) > ZEROI)
{
tempPtr->nextnode = curr;
if(curr == prev)
return tempPtr;
else
prev->nextnode = tempPtr;
return fPtr;
}
else
{
prev = curr;
curr = curr->nextnode;
}
}
prev->nextnode = tempPtr;
return fPtr;
}
thanks for replying to me
 
K

Keith Thompson

sulays said:
I need to insert and sort in a link list

Ok, well, good luck with that.

Don't expect much help unless you post an actual question and make
some effort to do it yourself.
 
N

Nick Keighley

please leave some context in your post (as I have)

in general when posting to comp.lang.c you should post a short
*complete* example
that illustrates your problem. I am left trying to gues what the
missing bits of your
program are.

in getting an infinite loop dont know why?

where is it looping? The easy way to answer this question is to use a
debugger or
put diagnostic print statements in your program.
this is in main
while(!feof(fpA))

bad idea

the usual C idiom is
while (fgets() != NULL)

lookup fgets() in your textbook for its parameters and semantics

{
readingaemployeedata(&fpA,lastn,firstn,midini,&empid,title,balance);

some comments would be nice...
if(feof(fpA))
break;
tempPtr =
creatingaemployeedata(lastn,firstn,midini,empid,title,balance);
while(tempPtr != NULL)

I havn't fully analysed your program (I can't because you didn't post
all of it...) but
this loop only exits if tempPtr is NULL. Your loop never modifies
tempPtr. I'm
assuming tempPtr isn't a global.

add a diagnostic to confirm this
printf ("tempPtr=%p\n", (void*)tempPtr);
prinheader();
firstemployeePtr = inserting(firstemployeePtr,tempPtr);
printing(firstemployeePtr);
}

function

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)
return tempPtr;

curr = prev = fPtr;

I think it would help if you explained what you are trying to do here.
It would probably
help you as well.
 
P

pete

sulays said:
if I use **fPtr insted of *fPtr I get a seg fault

Post something that I can compile.
If you feel that your program is too large to post,
create a smaller version that exhibits the same problem.
 
S

sulays

Im trying create a link list by accepting initial input from a file
<that part of my program is working ok>
the link list must be maintained in increasing alphabetical order base
on the employee's last name
employees with the same last name I need to listed in alphabetical
order based on the first name and if the have the same last name and
first name need to listed in ascending order based on employee id

trying to do the last name for now and see how it works

EIS* inserting(EIS *fPtr,EIS *tempPtr)
{
EIS *prev = NULL;
EIS *curr = NULL;

if(fPtr == NULL)< this is checking if the list is empty>
return tempPtr;

curr = prev = fPtr;<this is setting the other pointers to the first
node>
tempPtr->nextnode = curr; <this one is for the second node>
while(curr != NULL)
{
if(strcmp(tempPtr->lastname,curr->lastname) > ZEROI)
{
if(curr == prev)
return tempPtr;
else
prev->nextnode = tempPtr;
return fPtr;
}
else
{
prev = curr;
curr = curr->nextnode;
}
}
prev->nextnode = tempPtr;
return fPtr;
}



I dont know why is not working
 
S

sulays

yes is long and I dont know how to make small version think that
everything is necesary for the program but if you want I can
post the whole thing
 
N

Nick Keighley

LEAVE SOME CONTEXT IN YOUR POSTS!!

that is, please leave in the text of the part of the message you are
replying to.
My replies to your posts do this.
yes is long and I dont know how to make small version think that
everything is necesary for the program but if you want I can
post the whole thing

try a simpler problem. Try just building and printing a linked list for
instance.


int main (void)
{
List list;

insert (&list, "red"); /* add an item to the end of the list*/
insert (&list, "blue");
insert (&list, "green");

print (&list);

return 0;
}

this is the way to write a complicated program. Break it into simpler
steps
and solve the steps. When you have a simple program working then add
an extra feature. For instance you could modify insert to insert in the
correct
order,
 
S

sulays

like this? I dont know what you want i need hel with the inserting, is
only for last name for now but I need first name and mid ini and id
now i'm getting the first input to print but it stop
int main()
{
List list;
List temp;

while(!feof(fpA))
{
readingaemployeedata(fpA);
if(feof(fpA))
break;
temp = creatingaemployeedata();
while(temp != NULL)
{
list = inserting(list,temp);
printing(list);
}
setptrzero(list);
return 0;
}
}

List* inserting(EIS *list,EIS *temp)
{
List *prev = NULL;
List *curr = NULL;

if(list == NULL)
{
return temp;
}
else
{
curr = prev = list;
temp->next = curr;

while(curr != NULL)
{
if(strcmp(temp->lastname,curr->lastname) > ZEROI)
{
if(curr == prev)
return temp;
else
prev->next = temp;
return list;
}
else
{
prev = curr;
curr = curr->next;
}
}
prev->next = temp;
return list;
}
}
 
R

Randy Howard

sulays wrote
(in article
how do i do that?

Hint 1: Continuing to snip all relevant context from your posts
is going to piss everyone off.

Hint 2: Continuing to demonstrate zero self-effort at solving
your problems is going to piss everyone off.

Hint 3: We don't do your homework for you here, unless you post
contact information for your professor so we can contact him
directly with a solution.
 
S

sulays

I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway
 
M

Mark McIntyre

I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway

Didn't someone just say to you:

Hint 1: Continuing to snip all relevant context from your posts
is going to piss everyone off.

Hint 2: Continuing to demonstrate zero self-effort at solving
your problems is going to piss everyone off.

Hint 3: We don't do your homework for you here, unless you post
contact information for your professor so we can contact him
directly with a solution.
 
R

Richard Heathfield

sulays said:
like this? I dont know what you want i need hel with the inserting, is
only for last name for now but I need first name and mid ini and id
now i'm getting the first input to print but it stop

Thanks for showing your code. I compiled the code you posted, and here's the
compiler's output for you to study:

gcc -W -Wall -ansi -pedantic -O2 -g -pg -c -o foo.o foo.c
foo.c: In function `main':
foo.c:3: `List' undeclared (first use in this function)
foo.c:3: (Each undeclared identifier is reported only once
foo.c:3: for each function it appears in.)
foo.c:3: parse error before `list'
foo.c:6: warning: implicit declaration of function `feof'
foo.c:6: `fpA' undeclared (first use in this function)
foo.c:8: warning: implicit declaration of function `readingaemployeedata'
foo.c:11: `temp' undeclared (first use in this function)
foo.c:11: warning: implicit declaration of function `creatingaemployeedata'
foo.c:12: `NULL' undeclared (first use in this function)
foo.c:14: `list' undeclared (first use in this function)
foo.c:14: warning: implicit declaration of function `inserting'
foo.c:15: warning: implicit declaration of function `printing'
foo.c:17: warning: implicit declaration of function `setptrzero'
foo.c:20: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:22: parse error before `*'
foo.c:22: parse error before `*'
foo.c:23: warning: return-type defaults to `int'
foo.c:23: warning: type mismatch with previous implicit declaration
foo.c:14: warning: previous implicit declaration of `inserting'
foo.c:23: warning: `inserting' was previously implicitly declared to return
`int'
foo.c: In function `inserting':
foo.c:24: `List' undeclared (first use in this function)
foo.c:24: `prev' undeclared (first use in this function)
foo.c:24: `NULL' undeclared (first use in this function)
foo.c:25: `curr' undeclared (first use in this function)
foo.c:27: `list' undeclared (first use in this function)
foo.c:29: `temp' undeclared (first use in this function)
foo.c:38: `ZEROI' undeclared (first use in this function)
foo.c:55: warning: control reaches end of non-void function
make: *** [foo.o] Error 1
 
R

Randy Howard

sulays wrote
(in article
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway

Hint 4: Replace your ROM with RAM.
 
K

Keith Thompson

sulays said:
I posted what I have I just wanted to know what was wrong with it
because I can't see it being trying everything and stil not working
instead getting really confuse here with all the hints thanks anyway

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

This will automatically quote the previous message and add an
attribution line. The result will be similar to this followup, where
I've quoted what you wrote and indicated that you were the one who
wrote it.

We can't necessarily see the article to which you're replying. Each
followup needs to provide enough context so it can be read on its own.
(That doesn't mean you need to quote the entire article, just enough
for your followup to make sense.)

We have tried to tell you this several times, and you have
consistently failed to follow our advice. Please correct this;
otherwise, we'll probably give up on trying to help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top