Buggy C code

This is a discussion on Buggy C code within the C Programming forums, part of the General Programming Boards category; Hi yall i needed to write a program that reads all the words from a file and stores the unique ...

  1. #1
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    Buggy C code

    Hi yall i needed to write a program that reads all the words from a file and stores the unique words in a linked list and counting how many of them appear in the text. Here's my code but it seems to be a bit buggy and doesn't compile


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <ctype.h>
    # define MAX 10000
    
    
    typedef struct node{
    char word[50];
    int count;
    struct next *next;
       } node;
       
    node * makeNode( char arr[]);
    node * insertWord( char arr[],node* top);
    node * findPrevious (char arr[],node*top)
    void printWords(node * top);
    node * searchList (char arr[],node *top);
    //===========================MAIN================================//
    int main (){
        
    FILE * in=fopen("paragraph.txt","r");     
    
    int i=0,n,j;
    char str[MAX];
    node*top=NULL;
    node*found;
    
    
    
    if(in==NULL){
                printf ("File not found\n");
                
                }
       else {
               while ((fscanf (in,"%s" ,& str[i]))!=EOF){
                     fscanf (in,"%s" ,& str[i]);
                          i++;
                          }
                          
                 for (n=0;n<i;n++){
                     str[n]=tolower (str[n]);
                     }        //converts all the elements in the array to lower case
                     
              for (j=0;j<i;j++){
                  
                      found= searchList(str[j],top);//searchList function to search the list to see if the word previously occured
                          if (found==NULL){
                                           top=insertWord(str[j],top);/*insert the word in the list maintain sorted order*/
                                           }   
                                           
                                else {
                                     (found->count)++;// increment count since the word has previously occured before 
                                     }
                                     
                          }    //end for loop   
                                      
              printList(top);
              
         
              
              system ("pause");
              
              return 0;
              
              } //end main            
                          
    //=====================================END MAIN=====================================//
                                               
                          
    //==================================================================================//
    
    node* makeNode( char arr[n]){
          /* function to make a new node to store the word*/
            node* newNode;
            
            newNode= (node*) malloc (sizeof (node));
            strcpy(newNode->word,arr[n]);
            newNode-> count=1;
            newNode->next=NULL;
           
            return newNode;
            
            }//end makeNode
    
    //==================================================================================//
    
    
    node* insertWord( char arr[n],node* top){
        /* inserts the word in the list (maintaining sorted order) and return the address of top */ 
          node * previous;
          node * newNode;
                 
          newNode=makeNode(arr[n]);
          
          if (top==NULL){
                         top=newNode;
                         }
            else{
                         previous= findPrevious(arr[n],top);
                                 if (previous==NULL){
                                                     newNode->next=NULL;
                                                     top=newNode;
                                                     }
                                                   else{
                                                        newNode->next=previous->next;
                                                        previous->next=newNode;
                                                        }
                                                        
                                          }
                return top;
                
                
                }   /*end insertWord*/        
                
    //==================================================================================// 
     
     node* findPrevious (char arr[n],node*top){
                       
            //function to find where in the list to insert the new node created           
                       node * prev;
                       node * curr;               
                       
                while (curr!=NULL){
                            if ((strcmp (curr->word,arr[n])>0){// if the word on the linked list is higher in alphabetical value do//
                                        return prev;
                                        }
                                        else{
                                             prev=curr;//save previous before moving on      
                                               curr=curr->next;
                                               }
                                                 
                                            } //end while  
                                           return prev;
                                           
                                           }     // end findPrevious function       
                                                                                
    
    //==================================================================================//
    
                      
      void printWords(node * top) {
          /* prints all the words in the list 1 per line*/
          
            node * curr;
            curr=top;
            
            while (curr->next!=NULL){
    		printf(" The word ' %s ' occurs %d times\n\n", curr-> word, curr-> count);
    		
    		
    		curr=curr->next;
    		
                }//end while
                
            }//end printLIst
                 
    //==================================================================================// 
     
     node * searchList (char arr[int n],node *top){
             /* searches the list for a given word*/
        
            node *curr;
            
            *curr=*top;
            
         while (curr!=NULL){
                   if ((strcmp (curr->word,arr[n]))==0) { //if the word has occured in the list    
                   
                                    return curr;
                                    }
                                    curr=curr->next;
                                    } //end while                       
                                return NULL;//if the word has not occured in the list already    
                                
                                } // end searchList

    these are the errors i am getting:

    F:\Assignment 2\Assignment.c In function `findPrevious':
    21 F:\Assignment 2\Assignment.c syntax error before '{' token
    25 F:\Assignment 2\Assignment.c parameter `i' is initialized
    27 F:\Assignment 2\Assignment.c parameter `top' is initialized
    32 F:\Assignment 2\Assignment.c syntax error before "if"
    95 F:\Assignment 2\Assignment.c syntax error before "newNode"
    125 F:\Assignment 2\Assignment.c syntax error before "while"
    I am really at a point where i don't know what to do anymore. I can't seem to debug it myself

    Any help will be greatly appreciated

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    Code:
              } //end main
    end main?
    I don't think so.
    Indentation needs to be done properly.

    Lots of syntax errors:

    Code:
    node * findPrevious (char arr[],node*top);
    searchList() and insertWord() takes a char argument and not char *

    Still there are lot of syntax errors left and i believe you can find them all if you try to understand the compiler error messages.

    btw, what compiler are you using?
    I'm using Mingw port of GCC.
    It has lot more syntax errors than just the ones you reported.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    I'm using Dev C compiler
    I'll have to review my code again

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Alienchicken View Post
    I'm using Dev C compiler
    I'll have to review my code again
    Code:
    Run your code through an indentation tool. It should make the problem obvious. You're probably missing a '{' or '}' somewhere.
    
    I had to put this comment in a code block because the forum thinks that anything containing a '{' character is code. Blegh.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by brewbuck View Post
    '{' character is code. Blegh.
    there is also noparse tag
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22

    Thumbs up

    Ok thanks guys reviewed my program forgot to declare the fuctions at the top


    But I'm still getting this error

    [Warning] assignment from incompatible pointer type


    in all my functions where i search the next node.

    Code:
         node * curr=top;
                  
            
            while (curr->next!=NULL){
    		printf(" The word ' %s ' occurs %d times\n\n", curr-> word, curr-> count);
    		
    		                                  curr=curr->next;

    the error message is for the
    Code:
    curr=curr->next
    line


    Can someone please explain this error for me. I'd like to know why i get this error message


    btw thanks for the all the help.

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    Quote Originally Posted by Alienchicken View Post
    Ok thanks guys reviewed my program forgot to declare the fuctions at the top


    But I'm still getting this error





    in all my functions where i search the next node.

    Code:
         node * curr=top;
                  
            
            while (curr->next!=NULL){
    		printf(" The word ' %s ' occurs %d times\n\n", curr-> word, curr-> count);
    		
    		                                  curr=curr->next;

    the error message is for the
    Code:
    curr=curr->next
    line


    Can someone please explain this error for me. I'd like to know why i get this error message


    btw thanks for the all the help.
    Well, it would help if you post your recent reviewed code.
    Code:
    typedef struct node{
    char word[50];
    int count;
    struct next *next;
       } node;
    Anyway, my guess is that the error is because curr->next is of type struct next* while curr is of struct node* type.
    For a linked list you need a "self-referential" structure which means probably you need to change the "next" in red above to "node".
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    Ah yes, that's the problem there you're right. Thanks alot it really helped. My struct declaration was wrong.

  9. #9
    Registered User Alienchicken's Avatar
    Join Date
    Feb 2009
    Posts
    22
    Ok well all that is done but my program still doesn't compile i now get this error message

    [Linker error] undefined reference to `printList'
    [Linker error] undefined reference to `searchList'
    [Linker error] undefined reference to `insertWord'
    ld returned 1 exit status
    [Linker error] undefined reference to `findPrevious'
    Can some one explain what i am doing wrong there


    I am almost done with this prograam once i get this to work the program will most certianly compile successfully.


    btw I've made all the changes that you guys recommended earlier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  5. Debugging leads to buggy code and longer hours?
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21