x
Solved

# Why wont these functions work?

Posted on 2004-08-03
Medium Priority
268 Views
What is wrong with this function?
I was told that this function only inputted garbage and ai thought I understood but after trying all night to figure it out I am still wher I began.  Please tell me where to go.  I am trying to compare strings from a text  file to an array of store words and add to arry if unique or get next word if duplicate.  ai am also trying to eliminate cade discriminants and punctuation added
duplicates.  Here is the function I need help with.

int insertWord(char list[][MAX_SIZE], FILE *fp){

int count = 0, i=0;
char word[30];

while (fscanf(fp,"%s",list[i])!=EOF)i++ ;

for (i=0; word[i]; i++) {
if (!isalpha(word[i])) {
word[i] = 0;
break;
}
}

if (!foundWord(list, word,count))
strcpy (list[count++], word);
return count;
}

int  foundWord(char list[][MAX_SIZE],char word[], int size){

int i;

for(i=0;i<size;i++)
if (stricmp(list[i],word)==0) return 1;
return 0;
}

0
Question by:jholmes9186
• 4
• 3
• 3
• +1

Author Comment

ID: 11704914
Sorry, the above should say case discriminants.
0

LVL 46

Expert Comment

ID: 11705003
Hi jholmes9186,

You should explain to an invisble listener what the function does.

>     while (fscanf(fp,"%s",list[i])!=EOF)i++ ;
This reads strings into the list[] array

>      for (i=0; word[i]; i++) {
>              if (!isalpha(word[i])) {
>                  word[i] = 0;
>                  break;
>              }
>      }
This removes non-alpha words or so??? word[] hasn't been used yet!

>         if (!foundWord(list, word,count))
>             strcpy (list[count++], word);
Beats me what's the purpose...

>             return count;
Indentation wrong, but the final count is returned.

Somehow, I assume, word[] used to be filled, but did you remove that code?

Cheers!
Sjef
0

LVL 11

Expert Comment

ID: 11705078
Well, the variable "word" is never initialized.  So whatever block of memory is in it will be used for the search.

There's also the problem that the variable found may not be large enough to hold the file data.

Finally, the value of count is never initialized, so the find never even looks at a single element of the array, and will always fail and then copy the stuff in "word" (likely empty) over the first element in the array.

Hope this helps.
0

LVL 45

Expert Comment

ID: 11705097
usr char * [MAX_LIST] and allocate memory for each char * if you want this to work

char * list [ROWS];

for ( int i=0; i<ROWS; i++ )
{
list[i] = (char *) malloc (COLS);
if ( list[i] == NULL )
{
printf ("out of memory\n");
exit(1);
}
}
0

Author Comment

ID: 11705295
Sorry, I will clarify:

The first function insertWord is to read strings from an input txt file and calls foundWord to add the word being read to the array word and compare it with the list of unique words in list[][].  It should eliminate case variants as appearing to be separate words  (ie case and Case should be read as the same word) and also eliminate words with punctuation variants as being different words (ie word and word? should be read as the same word).  There is also a counter to display the total number of unique words.
0

LVL 11

Expert Comment

ID: 11705425
In that case, the first "while" needs ot encompass the entire function (short of the return).  It should read teh file data into the word variable, and then eliminate alphanumerics from word and call the foundWord function to find duplicates, finally adding the word if not found.

That should do it, assuming the list variable can fit all the strings.

0

LVL 45

Expert Comment

ID: 11705837
nope Kurt,
foundWord traverses the entire list

for (i=0; word[i]; i++) {
if (!isalpha(word[i])) {  << word array is uninitialized ... you need to read in something into this array before you start checking
word[i] = 0;
break;
}
}

if (!foundWord(list, word,count))   << this call to foundWord should pass count as number of elements in the list ... currently you are passing 0 .. replacing i with count in the first while loop should take care of this
strcpy (list[count++], word);
return count;
}
0

LVL 45

Expert Comment

ID: 11705934
oops you are right even the file can have duplicates

int insertWord(char list[][MAX_SIZE], FILE *fp){

int count = 0, i=0;flag=0;
char word[30];

while (fscanf(fp,"%s",word)!=EOF) {
for (i=0; word[i]; i++) {
if (!isalpha(word[i])) {
word[i] = 0;
flag=1;
break;
}
}
if ( flag )
{
flag =0;
continue;
}
if (!foundWord(list, word,count))
strcpy (list[count++], word);
}

int  foundWord(char list[][MAX_SIZE],char word[], int size){

int i;

for(i=0;i<size;i++)
if (stricmp(list[i],word)==0) return 1;
return 0;
}
0

Author Comment

ID: 11707009
Thanks for the help.  i have made the changes you suggested and tried to compile it.  I have come up with several errors.  It says that size, word, number are undefined identifiers.  It also gives a syntax error.  Here is the whole code.  Did I misunderstand something or does the rest of my program change the functions incorrectly??

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_SIZE 30

int  foundWord(char list [][MAX_SIZE],char word[],int size);
int insertWord(char list [][MAX_SIZE],FILE*fp);

int main(void) {

char list[1000][MAX_SIZE];
int wordcount;

FILE*fp;
fp=fopen("pgm6.txt","r");
if(fp==NULL){
printf("****ERROR**** input file cannot be read");
exit (1);
}

wordcount=insertWord(list,fp);

fclose(fp);

return 0;
}

int insertWord(char list[][MAX_SIZE], FILE *fp){

int count = 0, i=0;flag=0;
char word[30];

while (fscanf(fp,"%s",word)!=EOF) {
for (i=0; word[i]; i++) {
if (!isalpha(word[i])) {
word[i] = 0;
flag=1;
break;
}
}
if ( flag )
{
flag =0;
continue;
}
if (!foundWord(list, word,count))
strcpy (list[count++], word);
}

int  foundWord(char list[][MAX_SIZE],char word[], int size){

int i;

for(i=0;i<size;i++)
if (stricmp(list[i],word)==0) return 1;
return 0;
}

0

LVL 46

Expert Comment

ID: 11707194
Is this homework after all?
0

LVL 11

Accepted Solution

KurtVon earned 1500 total points
ID: 11707210
You're misisng a } at the end of insertWord.  You also need to return count at the end of insertWord.
0

LVL 45

Expert Comment

ID: 11712417
wow ?
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.