Getting a function to return a string

This is a discussion on Getting a function to return a string within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get a function to return a string with some user details in it, to a generic function ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    87

    Getting a function to return a string

    I'm trying to get a function to return a string with some user details in it, to a generic function that'll write it to a file. Does anyone know how to do this? My compiler doesn't like it when I do this
    Code:
    char Variable[100](int one, char two[20])
    Or does anyone know how to return more then one integer?
    **********************
    *==================*
    * Many Can. One Must... *
    *==================*
    **********************

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    An Information w.r.t Arrays

    You cannot pass arrays across functions and you cannot return the same.

    1) You could do good to make the array as part of a structure / class and then return the object of the struct / class

    2) You could dynamically allocate memory for the array and return the starting address of the array and receive the same using a pointer
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    CIS and business major
    Join Date
    Aug 2002
    Posts
    194
    to return a string:

    #include <string>
    using namespace std;

    string functionName()
    {}

    to output it to a file:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string stringName = "hello";
    
    	ofstream out("file2.txt");
    
    	out << stringName << endl;
    
    	out.close();
    
    	return 0;
    
    }

  4. #4
    CIS and business major
    Join Date
    Aug 2002
    Posts
    194
    Originally posted by Terrance
    to return a string:

    #include <string>
    using namespace std;

    string functionName()
    {}

    oops, meant:

    string functionName()
    {

    return stringName;
    }

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Cmon, use an std::string type! There's a ++ after that C, and don't forget that!

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    u can't return more than one value, you could use a reference parameter tho.

    void get_string(string &astring)
    {
    code;
    cin>>astring;
    }

    in main:

    string the_string;

    get_string(the_string);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM

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