help needed passing variables

This is a discussion on help needed passing variables within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have my program that loops through a chosen directory listing all of the files in it with ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    help needed passing variables

    Hi everyone, I have my program that loops through a chosen directory listing all of the files in it with the file path . However I would like to pass the string containing each file path to a new fiunction in my program so I can use that string to do processing on the file.

    Can anyone help me on how to pass the variable, I know its a simple question but im a bi of a beginner


    Code:
    int main(void)
     {
      DIR           *d;
      struct dirent *dir;
     
    cout << "Enter the name OF FOLDER: ";
    
            string g_1;
            getline( cin, g_1 );
            d = opendir ( g_1.c_str() );
      
    
    
    if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
          
     			string filepath;
                            string g =  dir->d_name;
    			string filename1 = g_1 + g;
    			cout << filename1<<endl;
    	
    
        }
    
        closedir(d);
      }
    
      return(0);
    }
    
    
    
    
    
    
    void processFile(ifsteam& file)
    {
           //I WOULD LIKE TO PASS MY STRING 'filename1' TO THIS FUNCTION
    }

    thank you

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    void processFile(ifsteam& file, const std::string& filename)

    processFile(file, filename);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thanks for your quick reply!

    I have changed it to how you advised, I believe this should now be passing the string 'filename1' to the function 'processFile'. I seem to be getting an error of 'undefined function 'processFile' in function main'. Am i doing something silly here?




    Code:
    
    
    
    if (d)
      {
        while ((dir = readdir(d)) != NULL)
        {
          
     			string filepath;
                            string g =  dir->d_name;
    			string filename1 = g_1 + g;
    			cout << filename1<<endl;
    	
    
    	
          processFile( filename1);
    
        }
    
        closedir(d);
      }
    
      return(0);
    }
    
    
    
    
    
    
    void processFile( const std::string& filename1)
    {
         
    //I will do stuff in here
      
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    You must create a prototype of the function and place it at the top of the file or in a header and include it.
    The compiler must be able to see the function or its prototype before you call it.
    A prototype is essentially just the header of the function, the first line and a ;

    void processFile( const std::string& filename1);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    thank you again!!

    can I just ask one more question then I promise thats me!!!

    Ive moved my function 'processFile' to above main and put a semicolon on the end of it but now im getting "declaration terminated incorrectly"

    Ahhhhhhhhh!!!!! Im sorry i know i must be doing something daft.......again!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    I hope you didn't do...
    Code:
    void processFile( const std::string& filename1);
    {
        // Code...
    }
    
    int main()
    {
        // Code...
    }
    ...because if so, then you did it wrong. You must do it this way:
    Code:
    void processFile( const std::string& filename1);
    
    int main()
    {
        // Code...
    }
    
    void processFile( const std::string& filename1)
    {
        // Code...
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Legend!!! you hit the nail right on the head, thats exactly what i was doing worng! thank you soo much! MUCH appreciated!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I hope you didn't do...
    Code:
    void processFile( const std::string& filename1);
    {
        // Code...
    }
    
    int main()
    {
        // Code...
    }
    ...because if so, then you did it wrong. You must do it this way:
    Code:
    void processFile( const std::string& filename1);
    
    int main()
    {
        // Code...
    }
    
    void processFile( const std::string& filename1)
    {
        // Code...
    }
    As long as you only have one processFile function, it is perfectly valid to put the whole definition of the processFile function before main - at this point there should be NO semicolon on the end tho'. Whether you should or shouldn't do this is a style decision mainly. Some people like to write functions "bottom up", meaning that the main is at the bottom of the file, and functions called by main and it's subfunctions are above.

    Others like to read the code from the top down, so you start the file with main and then all other functions go below main.

    There is really no technical advantage with one over the other - it's purely an aesthetic and personal (or, if you work as a programmer or study to become, perhaps a "style guide of the company/school/university") choice. There's a slight advantage with having the functions first and the calls later, and that is that you don't have to produce prototypes for functions in the same file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    If processFile is only to be used by main(), make it a static function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure variables into function
    By sarathius in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 11:56 PM
  2. Passing string variables
    By koolsafe in forum C Programming
    Replies: 4
    Last Post: 03-11-2006, 12:45 AM
  3. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 06:30 PM
  4. question on passing variables
    By dirgni in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2002, 03:36 PM
  5. help needed with passing objects by reference
    By finnepower in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2002, 03:38 PM

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