need help on function in bold causing program to crash:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct doc_words
{
int numWords;
char words[30];
struct doc_words *next;
};
char boolean = 'F';
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])
{
newNode=getmem();
strncpy(newNode->words,text, 29);
newNode->numWords=0;
newNode->next=head;
head=newNode;
}
void check (char word[30])
{
if (boolean =='F')
{
AddWords( word);
boolean ='T';
goto s;
}
current=head;
// char temp[30];
//char a[30];
// strncpy(temp,word,29);
// strncpy( a,current->words,29);
printf("the word current is: %s and count = %d",current->words,current->numWords);
do
{
if ((strcmp(current->words,word))==0)
//if ((strcmp(a,temp))==0)
{
current->numWords++;
break;
}
else
{
current=current->next;
}
}while (current!=NULL);
if(current->next==NULL)
{
AddWords( word);
}
s:
printf("");
}
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);
check(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;
}
Enter your message below
Sign in or Join us (it's free).