Solved

Why wont these functions work?

Posted on 2004-08-03
12
257 Views
Last Modified: 2011-09-20
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
Comment
Question by:jholmes9186
  • 4
  • 3
  • 3
  • +1
12 Comments
 

Author Comment

by:jholmes9186
ID: 11704914
Sorry, the above should say case discriminants.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
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

by:KurtVon
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Expert Comment

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

Instead of char list[ROW][COLS], use
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

by:jholmes9186
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

by:KurtVon
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

by:sunnycoder
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

by:sunnycoder
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

by:jholmes9186
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

by:Sjef Bosman
ID: 11707194
Is this homework after all?
0
 
LVL 11

Accepted Solution

by:
KurtVon earned 500 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

by:sunnycoder
ID: 11712417
wow ?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question