string variable

Discussion in 'C++' started by cdg, Mar 7, 2006.

  1. cdg

    cdg Guest

    Could anyone tell me how to correctly assign a variable name to the return
    of the function call in this example program. I needed to convert a integer
    to a string, using stringstream. However after returning the string I am not
    sure how to assign a variable name to it.

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

    string Convert(int);

    void main ()
    {
    int num = 123456789;

    "???" = Convert(num).c_str() //***variable here***
    }

    string Convert(int num)
    {
    stringstream test;
    test << num;

    return test.str();
    }
     
    cdg, Mar 7, 2006
    #1
    1. Advertising

  2. cdg wrote:
    > Could anyone tell me how to correctly assign a variable name to the return
    > of the function call in this example program. I needed to convert a integer
    > to a string, using stringstream. However after returning the string I am not
    > sure how to assign a variable name to it.
    >
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > string Convert(int);
    >
    > void main ()
    > {
    > int num = 123456789;


    Here you declare a variable, named 'num', of type 'int', and initialise
    it with a number. Seems simple enough, no?

    >
    > "???" = Convert(num).c_str() //***variable here***


    Here, declare another variable, name it something, give it a type
    (an appropriate type), and initialise it with a call to 'Convert'.

    Now, give it your best shot.

    > }
    >
    > string Convert(int num)
    > {
    > stringstream test;
    > test << num;
    >
    > return test.str();
    > }
    >
    >


    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 7, 2006
    #2
    1. Advertising

  3. cdg

    cdg Guest

    Thanks.

    I have just started using strings with stringstream, and didn`t realize it
    would be quite that simple.
     
    cdg, Mar 7, 2006
    #3
  4. cdg

    red floyd Guest

    cdg wrote:
    > Could anyone tell me how to correctly assign a variable name to the return
    > of the function call in this example program. I needed to convert a integer
    > to a string, using stringstream. However after returning the string I am not
    > sure how to assign a variable name to it.
    >
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > string Convert(int);
    >
    > void main ()
    > {
    > int num = 123456789;
    >
    > "???" = Convert(num).c_str() //***variable here***
    > }
    >
    > string Convert(int num)
    > {
    > stringstream test;
    > test << num;
    >
    > return test.str();
    > }
    >


    Big hint.... What does std::string::c_str() return? That should give
    you an idea as to the variable type.
     
    red floyd, Mar 7, 2006
    #4
  5. cdg

    cdg Guest

    Could anyone help me with this next problem I am having with this same
    example program. I need to get the the string length, and there is an error
    message at the "strlen" line.

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

    string Convert(int);

    void main ()
    {
    int num = 123456789;
    int tmlen(0);
    string result;

    result = Convert(num).c_str();

    reslen = strlen(result);//***problem here***

    cout<<reslen<<endl;
    }

    string Convert(int num)
    {
    stringstream test;
    test << num;

    return test.str();
    }
     
    cdg, Mar 8, 2006
    #5
  6. cdg

    Jim Langston Guest

    "cdg" <> wrote in message
    news:EspPf.10426$Tf3.5711@dukeread09...
    > Could anyone help me with this next problem I am having with this same
    > example program. I need to get the the string length, and there is an
    > error
    > message at the "strlen" line.
    >
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > string Convert(int);
    >
    > void main ()
    > {
    > int num = 123456789;
    > int tmlen(0);
    > string result;
    >
    > result = Convert(num).c_str();


    just do result = Convert(num)
    or even better yet
    std::string result = Convert(num);

    > reslen = strlen(result);//***problem here***


    result.length();

    > cout<<reslen<<endl;
    > }
    >
    > string Convert(int num)
    > {
    > stringstream test;
    > test << num;
    >
    > return test.str();
    > }
    >
    >
     
    Jim Langston, Mar 8, 2006
    #6
  7. cdg

    BobR Guest

    cdg wrote in message ...
    > Could anyone help me with this next problem I am having with this same
    >example program. I need to get the the string length, and there is an error
    >message at the "strlen" line.
    >
    >#include <iostream>
    >#include <sstream>
    >using namespace std;
    >
    >string Convert(int);
    >
    >void main (){


    int main(){ // ALWAYS returns 'int'

    > int num = 123456789;
    > int tmlen(0);

    // > string result;
    // > result = Convert(num).c_str();

    std::string result = Convert( num ).c_str();

    // > reslen = strlen(result);//***problem here***

    size_t reslen( result.size() );

    > cout<<reslen<<endl;


    return 0;
    >}
    >
    >string Convert(int num){
    > stringstream test;
    > test << num;
    > return test.str();
    >}


    You don't have a book, do you?

    Get "Thinking in C++", 2nd ed. Volume 1 by Bruce Eckel
    (available for free here. You can buy it in hardcopy too.):
    http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

    Then off to:
    www.accu.org

    --
    Bob R
    POVrookie
     
    BobR, Mar 8, 2006
    #7
  8. cdg

    Peter_Julian Guest

    "cdg" <> wrote in message
    news:EspPf.10426$Tf3.5711@dukeread09...
    | Could anyone help me with this next problem I am having with this same
    | example program. I need to get the the string length, and there is an
    error
    | message at the "strlen" line.
    |
    | #include <iostream>
    | #include <sstream>
    | using namespace std;
    |
    | string Convert(int);
    |
    | void main ()
    | {
    | int num = 123456789;
    | int tmlen(0);
    | string result;
    |
    | result = Convert(num).c_str();
    |
    | reslen = strlen(result);//***problem here***
    |
    | cout<<reslen<<endl;
    | }
    |
    | string Convert(int num)
    | {
    | stringstream test;
    | test << num;
    |
    | return test.str();
    | }
    |

    A std::string knows it own size().

    #include <iostream>
    #include <ostream>
    #include <sstream>
    #include <string>

    template< class T >
    std::string Convert(const T& t)
    {
    std::eek:stringstream oss;
    oss << t;
    return oss.str();
    }

    int main()
    {
    int n = 12345;
    std::string s = Convert<int>(n);
    std::cout << "\nsize of s = " << s.size();
    std::cout << "\ns = " << s;

    s += Convert<int>(6789);
    std::cout << "\nsize of s = " << s.size();
    std::cout << "\ns = " << s;

    return 0;
    }

    /*
    size of s = 5
    s = 12345
    size of s = 9
    s = 123456789
    */
     
    Peter_Julian, Mar 8, 2006
    #8
  9. cdg

    benben Guest

    > Big hint.... What does std::string::c_str() return? That should give
    > you an idea as to the variable type.


    I hope you haven't misled the OP much.

    std::string::c_str() returns a const char*. But if you do the following:

    const char* s = Convert(num).c_str();

    then s will point to invalid memory. To avoid this, copy the content
    before it gets destroyed:

    string s1 = Convert(num);
    string s2 = Convert(num).c_str();

    The call to c_str() is redundant.

    Regards,
    Ben
     
    benben, Mar 8, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mfglinux
    Replies:
    11
    Views:
    703
    Roberto Bonvallet
    Sep 12, 2007
  2. Mir Nazim
    Replies:
    2
    Views:
    348
    Mir Nazim
    Dec 21, 2008
  3. cayenne
    Replies:
    19
    Views:
    666
    John W. Krahn
    May 20, 2004
  4. David Filmer
    Replies:
    19
    Views:
    245
    Kevin Collins
    May 21, 2004
  5. Stefan Mueller
    Replies:
    9
    Views:
    130
    Stefan Mueller
    Nov 1, 2005
Loading...

Share This Page