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:

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

}

i am having this compiler error:

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

for this line of code:

if (strcmp(check->words,text[30])==0)

This is valid if (strcmp(check->words,text)==0) and this is not if (strcmp(check->words,text[30])==0)

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:

if (strcmp(check->words,temp)==0)

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.

temp has value of : "is"

check->value has avue of: Could not watch this variable

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              
      {
     [B]  if (current->words==temp)[/B]
          {
            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;

}

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

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

test16.cpp: In function `void AddWords(char*)':
test16.cpp:54: parse error before `else'
test16.cpp:60: break statement not within loop or switch
test16.cpp: At global scope:
test16.cpp:62: parse error before `}' token

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.

do 
{
if (current->words==temp)
{
current->numWords++;
break;
}
check=check->next; 
}while (check!=NULL);
else
{

You've nested your loops wrong:

do a
if b
end a
else b
end b

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.