passing variables

This is a discussion on passing variables within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I have two newbie questions, is it possible to pass an ifstream variable to a function the same ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    passing variables

    Hey guys, I have two newbie questions, is it possible to pass an ifstream variable to a function the same way you can pass an integer variable?

    I was also wondering when cin.getline is used as follows is it better to use a string or an array?

    Thanks in advance for any advice.

    Code:
    char ch[10000];
    cout<<"enter something"<<endl;
    cin.getline(ch, 10000);

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by 182
    Hey guys, I have two newbie questions, is it possible to pass an ifstream variable to a function the same way you can pass an integer variable?
    Yes, you'll usually want to pass it as a non-const reference argument since any changes to the stream (the state of the stream) made within the function should be kept when the function exits.

    Quote Originally Posted by 182
    I was also wondering when cin.getline is used as follows is it better to use a string or an array?
    Well, you can use either version depending on your needs; but, cin.getline() is only for character arrays, the string form of that function looks a little bit different: getline(cin,name_of_string).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >is it possible to pass an ifstream variable to a function the same way you can pass an integer variable?
    Yes, but you should pass a reference (&). In fact some compilers give you an error if it's not a reference or pointer.
    Code:
    void foo( ifstream &in )
    >I was also wondering when cin.getline is used as follows is it better to use a string or an array?
    A string is the modern C++ way:
    Code:
    string ch;
    cout<<"enter something"<<endl;
    getline(cin, ch);

  4. #4
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for your replies I was also wondering if I was using an array to loop through a txt files contents with the following code where word is the array and test2 is the ifstream variable could i also use a string instead of an array in this case or am I best keeping the array?

    Thanks again guys.

    Code:
    char word[10000];
    while ((test2>> word))

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Yes, you could use a string object in place of the character array by simply substituing the string variable name in place of the character array name. One whitespace separated word would be read each iteration through the loop. A string object would be more space efficient taking up less stack space.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    IMO, best to switch to string:
    Code:
    string word;
    while ((test2 >> word))
    The most obvious reason being you don't have to allocate space for 10000 characters, and if the user types in more characters than you allocate space for the string code will still work just fine.

  7. #7
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks again guys your advice is great, I will use strings this will be my first time using them. So as you can implement a string by using the code string name; does it have any limit in size as it doesn't have an index like an array?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    To see what the theoretical maximum size for a string is, use the max_size member function.

    Code:
    string str;
    cout << str.max_size() << endl;
    My output:
    Code:
    4294967293
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks guys
    Last edited by 182; 02-15-2006 at 01:38 PM.

  10. #10
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by swoopy
    >is it possible to pass an ifstream variable to a function the same way you can pass an integer variable?
    Yes, but you should pass a reference (&). In fact some compilers give you an error if it's not a reference or pointer.
    Code:
    void foo( ifstream &in )
    When I use code like the above I get an error which says cannot convert parameter 1 from the class ifstream * to class ifstream &. Anyone have anyidea what the problem could be?

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    What does your function call look like?

    Code:
    #include <fstream>
    
    void PASS_IFSTREAM(std::ifstream &foo) {}
    
    int main() 
    {
        std::ifstream foo;
        
        PASS_IFSTREAM(foo);   // not PASS_IFSTREAM(&foo)
    }
    Last edited by SlyMaelstrom; 02-15-2006 at 01:50 PM.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Anyone have anyidea what the problem could be?
    Leave off the & when calling the function. The compiler already knows it's a reference, when it sees the function's definition.
    Code:
    foo( name_of_ifstream );

  13. #13
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    That was it thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Passing variables to a subroutine
    By shaig in forum C Programming
    Replies: 5
    Last Post: 03-16-2009, 06:21 PM
  3. C# in ASP.Net - Passing complex variables
    By Llam4 in forum C# Programming
    Replies: 3
    Last Post: 01-19-2008, 01:53 AM
  4. question on passing variables
    By dirgni in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2002, 03:36 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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