Thread: returning a string from a function

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    returning a string from a function

    howdy
    i'm trying to figure iut how to return a string from a function. have i missed something in the following code???

    ----------code---------------



    #include <iostream>
    #include <string>
    using namespace std;

    string stringy ()
    {
    string string1;
    cout<<"Enter a string"<<endl;
    getline(cin, string1);
    return string1;
    }


    int main ()
    {
    int number;
    string string1;

    stringy();
    cout<<"How many times?\n";
    cin>> number;

    for (int index = 0; index < number; index++)
    {

    cout<<index <<">--->>>"<< string1 <<"<<<--<"<< index<<endl;
    }
    return 0;
    }


    i wrote this in emacs and am running rh 2.4.7?
    it seems to compile allright but when run it will not place the entered string (string1) into the line between the index numbers.

    thanks from a sel teaching newbie
    M.R.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Looks like a simple case of ignoring return result

    Try
    string1 = stringy();

  3. #3
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    Howdy Salem,
    Very Cool.
    this return thing is a bit confusing. next it will pointers and adresses.

    Thanks
    M.R.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Exclamation

    I'll give an advice. If you write a function that returns a value, no matter what kind of value is, you should NEVER use this function to print any text out on the screen. It's not healty for your program.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    Howdy GaPe,

    do i understand you to say this piece -
    cout<<"Enter a string"<<endl; - would go in main or somewhere else.
    M.R.

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    YES, in the main or into some other function that returns nothing.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Returning a string from function
    By badmantel in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 12:17 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM