quick question - string

Posted on 2005-04-23
Last Modified: 2012-05-05
hello experts,

I'm very beginner in C++ so I need helpwith two problems.
problem 1:
I have non-void function which returs string:

string TestString(int Integer){
        return "test";

I declared function prototypes and everything works fine until I want to cout this string.

cout << TestString(1);

I want to make this function to read integer from 1-12 and return string  for example:
and so on...

problem 2:
how can I connect string and variable together and print it out. I what to make error message consist from string(text) and variables is it possible?


string errormesage = "here some text " AND variable AND " again some text";

thank you


Question by:Luxana
    LVL 86

    Accepted Solution

    The following code snippet addresses both of your questions:

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

    string TestString(int Integer){
          return "test";

    int main () {

    cout << TestString(1).c_str() << endl;

    int variable = 42;

    stringstream errormessage;

    errormessage << "here some text " << variable << " again some text";

    cout << errormessage.str().c_str() << endl;

    return 0;

    To the 1st one, you need to call 'string::c_str()' to use the correct operator for 'cout', to the 2nd one, the most efficiant and flexible way to do the formatting you suggested is using a stringstream, as demonstrated above. You can use it just like any other stream and access the contents as shown when you're done.
    LVL 10

    Author Comment


    thanks for help. I started using unix g++ compiler and everything is working there without c_str() and #include <sstream> . ANyway I will try your solution as soon as I get back  to windows because I definitely need to make sure that my program can be compiled with visual C++ as well.

    thanks again
    LVL 10

    Assisted Solution

    The first part should work without any changes

    When u return a string object, actually a temporary string object gets cnstructed and is returned.
    And every temporary object in C++ is implicitly const.
    Hence, acually, the object that gets subsstituted in the cout << is a const string.

    Now, << is actally an overloaded operator and the syntax  is

    ostream& operator<<( ostream& os, const string& s );

    Now, as u can see, the << operator expects a reference to an ostream type of object on its left hand side (i.e. cout in your case) and a reference to a const string on right hand side (thats the returned string object in your case)
    Hence it should work perfectly fine and It does with GNU compiler, as per the C++ standards

    Also, regadring your 2nd question, u will  have to use stringstream object as shown above
    Below is a reference for the same


    LVL 15

    Assisted Solution

    As Amit implied, you shouldn't need to call the c_str function to put a string out to a stream.

    Possibly your problem was that output to the stream does not appear until you send an endl as in jkr's example.  Writing a '\n' character might also work, but an endl should definitely work.  Until the program writes an endl, the output data sits in a buffer, but may not be visible on the output device or window.

    If that's not the problem, I think we would need to see more code to advise you further.
    LVL 10

    Author Comment

    thanks guys for help this fix the problem: #include <sstream> it is interenstig because g++ compiler get my code work without  preprocessor directive <sstream>. Sys_Prog  was right that I do not need to do any changes so I accept his answer && jrk's && efn as assist .

    thanks again

    LVL 10

    Author Comment

    oops I did accepts jrk's answer I will ask page editor to change it.


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now