Thread: Passing & Returning Arrays

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Passing & Returning Arrays

    Hi, I am trying to figure out why I cannot get my script to compile. My compiler says I have an undefined reference to my functions. Could someone tell me what it is I am missing?

    Code:
    #include <iostream>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h>
    
    char removePunct(char);
    char removeSpace(char);
    
    
    
    int main()
    {
     const int MAXLENGTH = 25;
     const int MAXCHARS = 71;
     const int MAXLINES = 7;
     char record[MAXLENGTH] = "record.txt";
     char line[MAXCHARS];
     int ch, i;
     char entry;
     ifstream in_file;
     in_file.open(record); 
    
    
    
    	  cout << "				Simple Character Processor " << endl;
          cout << endl;
          cout << "(R) . Read a new line from the file. " << endl;
          cout << "(P) . Print the current state of the line. " << endl;
          cout << "(M) . Remove all punctuation marks from the line. " << endl;
          cout << "(S) . Remove all spaces from the line. " << endl;
          cout << "(D) . Determine if the line is a palindrome. " << endl;
          cout << "(W) . Determine and output all starting positions of a given word in a line. " << endl;
          cout << "(Q) . Quit the program. " << endl;
          cout << endl;
          cout << "Enter Option > "; 
          cin >> entry;
          cout << endl;
    
       while (entry != 'q' || entry != 'Q')
       {
    	  	 
    	  if (entry == 'r'|| entry == 'R')
    	  	 {
    
    		 while((ch = in_file.peek()) != EOF )
    	 	    {
         	 	in_file.getline(line, MAXCHARS, '\n');
         	 	break;
         	 	} //end while loop
    			
    			getch();
    		 }
          else if (entry == 'p' || entry == 'P')
          	 {
    		   cout << "The current state of the line is: " << endl;
    		   cout << endl;
    		   cout << line << endl;
    		   }
          else if (entry == 'm' || entry == 'M')
     	  {
    	   line[MAXCHARS] = removePunct(line[MAXCHARS]);
    	  }
          else if (entry == 's' || entry == 'S')
           line[MAXCHARS] = removeSpace(line[MAXCHARS]);
             
          else if (entry == 'd' || entry == 'D')
             cout << "D works. " << endl;
             
          else if (entry == 'w' || entry == 'W')
             cout << "W works. " << endl;
             
          else if (entry == 'q' || entry == 'Q')	  
          	   break;
          
    	  getch();
    	  
    	  
    	  }
    
    	  in_file.close();	  	  
    	  getch();
       	  return 0;
    
    } // end main
    
    char removePunc(char words)
    {
       
     const int MAXCHARS = 71;
     char i;
     
     	 
     for(i=0; i < MAXCHARS; i++)
     {
      		  if ((words < 32) || ((words > 33) && (words < 65)) || ((words > 90) && (words < 97)) || (words >122))
    		  {
    		  words = 32;
     		  }
     		   
     }// end i loop
    
     
     
     return(words);
       
    }// end removepunc function
    
    char removePunc(char words[])
    {
     const int MAXCHARS = 71;
     
     int i, j;
     j=0;	 
     	 for(i=0; i < MAXCHARS; i++)
     	 {
    	  
      		  if (words[i] != ' ')
    		  
    		  words[j++] = words[i]  ;
    	 }//end i loop
       
    
     return(words[i]);
     
    } //end removeSpace function

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jeffdavis_99
    Hi, I am trying to figure out why I cannot get my script to compile. My compiler says I have an undefined reference to my functions. Could someone tell me what it is I am missing?
    Your compiler has already told you:

    You are missing the definitions of functions removePunct() and removeSpace(). You prototyped them and you invoked them, but you never defined them. (At least not in this file.)

    Regards,

    Dave

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Quote Originally Posted by Dave Evans
    Your compiler has already told you:

    You are missing the definitions of functions removePunct() and removeSpace(). You prototyped them and you invoked them, but you never defined them. (At least not in this file.)

    Regards,

    Dave
    How did I not define them? I thought they were defined on the first line of the function.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jeffdavis_99
    How did I not define them? I thought they were defined on the first line of the function.
    You have declared that they are functions that take a char as an argument and return a char. So the compiler knows how to implement the function calls and to make the returned value available to the calling program.

    You have not implemented the functions. Where is the code that performs whatever task the functions are supposed to do? The linker can't find them. That's what I mean by "not defined" and that's what the message "undefined reference" means.

    Regards,

    Dave

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    you should use a switch for that instead of while letter is this and this.....

    Code:
    switch (entry)
    {
       case 'R': case 'r':
            statement;
            break;
       case 'P': case 'p':
            statement;
            break;
       case 'M': case 'm':
            statement;
            break;
       case 'S': case 's':
            statement;
            break;
       case 'D': case 'd':
            statement;
            break;
       case 'W': case 'w':
            statement;
            break;
       case 'Q': case 'q':
            exit = true;      //put program in a while that states while(!exit)
            break;
       default:
            cout<< "Invalid entry...please re-enter" << endl;
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I guess I don't under stand this. I made the functions seperately to make sure they worked and feed them both character strings to test them. They performed the tasks that I coded them to do. The functions are returning a value to be assigned to "line" ie,

    line[MAXCHARS] = removePunct(line[MAXCHARS]);

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jeffdavis_99
    I guess I don't under stand this. I made the functions seperately to make sure they worked and feed them both character strings to test them. They performed the tasks that I coded them to do. The functions are returning a value to be assigned to "line" ie,

    line[MAXCHARS] = removePunct(line[MAXCHARS]);
    What compiler are you using? You have to tell the compiler about all of the files that are part of your project.

    So, for example, with g++, if your main program is in a file named main.cpp and your functions are in funcs.cpp, then if you do this:

    g++ main.cpp
    You will get some kind of "undefined reference" error message.

    What you could do is something like this:
    g++ main.cpp funcs.cpp
    If you are using a GUI such as dev-cpp or VisualStudio, you have to make all of the source files part of the project.

    If you don't understand what I am getting at, then tell us exactly what files you have and how you are compiling them (what Operating System, what compiler.)

    Regards,

    Dave

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I am using dev-cpp. The only file I am using is a .txt file that has a list of palindromes that the main function is reading in. Are you saying that I should have a specific #include that I did not list on my original posting?

  9. #9
    Red Panda basilisk's Avatar
    Join Date
    Aug 2001
    Posts
    219
    Reading your opening post you have defined your functions as:

    Code:
    char removePunct(char);
    char removeSpace(char);
    However when you have used them within your main file you have the following:

    char removePunc(char words)
    (ie missing the t at the end of Punc)

    and you have also called removePunc again for the removeSpace function.
    ie this bit:
    Code:
    char removePunc(char words[])
    {
     const int MAXCHARS = 71;
     
     int i, j;
     j=0;	 
     	 for(i=0; i < MAXCHARS; i++)
     	 {
    	  
      		  if (words[i] != ' ')
    		  
    		  words[j++] = words[i]  ;
    	 }//end i loop
       
    
     return(words[i]);
     
    } //end removeSpace function
    Is this a typo when you wrote your post or did you copy and paste from your source code
    Do not meddle in the ways of dragons, for thou art crunchy and taste good with ketchup

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jeffdavis_99
    I am using dev-cpp. The only file I am using is a .txt file that has a list of palindromes that the main function is reading in. Are you saying that I should have a specific #include that I did not list on my original posting?
    I am really sorry that I didn't see exactly what you had in your file.

    You do not have a function named removePunct() or a function named removeSpace() defined in your file. That's what the compiler is complaining about. Somehow I thought they must be in separate files that should have been part of your project.

    Now that I am looking, however, I see that you have two functions named removePunc(). An obvious copy-and-paste error that you should be able to fix.

    Sorry about my previous not-very-helpful responses.

    Regards,

    Dave

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    You guys are missing the fact that he has the function prototyped as
    Code:
    char removePunct(char);
    but then has it defined as

    Code:
    char removePunc(char* )
    Note the parameters and the spelling error (which has already been addressed).

    Or I don't know what the hecks going on. He has 2 functions defined with the same exact signature. You can't do that either.
    Last edited by homeyg; 04-02-2005 at 06:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Multidementional arrays
    By kingneb in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2008, 03:24 PM
  2. Replies: 3
    Last Post: 11-01-2007, 01:01 AM
  3. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 10:25 AM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. passing arrays to functions
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 03:18 PM