944,080 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2118
  • C RSS
Oct 24th, 2006
0

help me on linked list

Expand Post »
the function first search a word from the linked list. if found it then increment the numWords (number of words in listfor that word) and if not found it shoud create a new node for the word.

the code is:

  1. void AddWords( char text[30])
  2. {
  3.  
  4. check=head;
  5.  
  6. while (check!=NULL)
  7. {
  8. if (strcmp(check->words,text[30])==0)
  9. {
  10. check->numWords++;
  11. break;
  12. }
  13. else
  14. {
  15. newNode=getmem();
  16. strncpy(newNode->words,text, 29);
  17. newNode->next=head;
  18. head=newNode;
  19. }
  20. check=check->next;
  21.  
  22. }


i am having this compiler error:

Warning] passing arg 2 of `strcmp' makes pointer from integer without a cast

for this line of code:

  1. if (strcmp(check->words,text[30])==0)
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 24th, 2006
0

Re: help me on linked list

This is valid
if (strcmp(check->words,text)==0)
and this is not
if (strcmp(check->words,text[30])==0)
Last edited by andor; Oct 24th, 2006 at 9:05 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: help me on linked list

i tried that but when debugging i am having this error

An Access violation(segment fault) raised in your program.

the line of code crushinhg the program is:

Quote ...
if (strcmp(check->words,temp)==0)
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 24th, 2006
0

Re: help me on linked list

I asume that temp is typo error becouse its text not temp. Is check->words valid? When you debuging what are the values for check->words and for text.
Last edited by andor; Oct 24th, 2006 at 9:50 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: help me on linked list

temp has value of : "is"

check->value has avue of: Could not watch this variable
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 24th, 2006
0

Re: help me on linked list

here is the whole program:
line in bold when debugged is giving the value of currunt->words as Could not watch this variable

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

struct doc_words
{
char words[30];
int numWords;
struct doc_words *next;
};

struct doc_words *head=NULL;;
struct doc_words *current;
struct doc_words *check;
struct doc_words* newNode;

//char *calloc();

struct doc_words *getmem()
{
struct doc_words *p;
//p = malloc(sizeof(struct doc_words));
p = (struct doc_words*)malloc(sizeof(struct doc_words));
if(!p)
{printf("out of memory");
return(0);
}
else
return(p);
}

void AddWords( char text[30])
{
 
current=head;
 char temp[30];
 strncpy(temp,text,29);
      do              
      {
       if (current->words==temp)
          {
            current->numWords++;
            break;
          }
       check=check->next;     
      }while (check!=NULL);
       else
           {                                
             newNode=getmem();
             strncpy(newNode->words,text, 29);
             newNode->next=head;
             head=newNode;
             break;                                
           }
      
}

void extract_words_from_file()
{

char word[30];

FILE *ifp;

if ((ifp=fopen("C:\\collins_tirivamwe\\DSA\\text.txt","r"))!=NULL)
{

while (fscanf(ifp,"%s",word)!=EOF)

{
printf("%s",word);
AddWords(word);
}
fclose(ifp);
}
else
printf("error");
}

void read()
{
printf("words in list");
struct doc_words *current= head;
while (current!=NULL)
{

printf("%s\n",current->words);
current=current->next;
}

}

int getCount()
{
int count=0;
struct doc_words *current= head;
while (current!=NULL)
{
count++;
current=current->next;

}

printf("\n %d",count);
return (count);
}

/*
int getCount(char word[30])
{
int count=0;
struct doc_words *p = head;
while (p!=NULL)
{
if (strcmp(p->word,word)==0)
{
count++;
p=p->next;
}
else
{
p=p->next;
}
}
return(count);
}



*/

int main()
{
int p=0;
extract_words_from_file();

p=getCount();
// printf("\nnumber of words are %d",p);
read();
system("PAUSE");
return 0;

}
Last edited by ~s.o.s~; Oct 24th, 2006 at 2:24 pm.
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 24th, 2006
0

Re: help me on linked list

Click to Expand / Collapse  Quote originally posted by tirivamwe ...
here is the whole program:
line in bold when debugged is giving the value of currunt->words as Could not watch this variable
Wont compile. Fix the errors and read this
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: help me on linked list

When I post something to this thread its not there. Whats happening here.
FOR tiriamwe: Ok fix the code becouse it wont compile. Read this. Posting errors of your prog
  1. test16.cpp: In function `void AddWords(char*)':
  2. test16.cpp:54: parse error before `else'
  3. test16.cpp:60: break statement not within loop or switch
  4. test16.cpp: At global scope:
  5. test16.cpp:62: parse error before `}' token
  6.  
EDIT: Its ok now but there is some kind a bug when posting, becouse if I reply to 6th post of this thread my post is missing.
Last edited by andor; Oct 24th, 2006 at 11:15 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 24th, 2006
0

Re: help me on linked list

Quote ...
  1. do
  2. {
  3. if (current->words==temp)
  4. {
  5. current->numWords++;
  6. break;
  7. }
  8. check=check->next;
  9. }while (check!=NULL);
  10. else
  11. {
You've nested your loops wrong:

do a
if b
end a
else b
end b
Last edited by Nick Evan; Mar 26th, 2010 at 9:41 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Keypress Detection
Next Thread in C Forum Timeline: can anyone give me another way of solvin this problem?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC