string variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cdg

    #1

    string variable

    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();
    }


  • Victor Bazarov

    #2
    Re: string variable

    cdg wrote:[color=blue]
    > 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;[/color]

    Here you declare a variable, named 'num', of type 'int', and initialise
    it with a number. Seems simple enough, no?
    [color=blue]
    >
    > "???" = Convert(num).c_ str() //***variable here***[/color]

    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.
    [color=blue]
    > }
    >
    > string Convert(int num)
    > {
    > stringstream test;
    > test << num;
    >
    > return test.str();
    > }
    >
    >[/color]

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • red floyd

      #3
      Re: string variable

      cdg wrote:[color=blue]
      > 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();
      > }
      >[/color]

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


      Comment

      • cdg

        #4
        Re: string variable

        Thanks.

        I have just started using strings with stringstream, and didn`t realize it
        would be quite that simple.


        Comment

        • cdg

          #5
          Re: string variable

          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<<e ndl;
          }

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

          return test.str();
          }


          Comment

          • Jim Langston

            #6
            Re: string variable

            "cdg" <anyone@anywher e.com> wrote in message
            news:EspPf.1042 6$Tf3.5711@duke read09...[color=blue]
            > 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();[/color]

            just do result = Convert(num)
            or even better yet
            std::string result = Convert(num);
            [color=blue]
            > reslen = strlen(result);//***problem here***[/color]

            result.length() ;
            [color=blue]
            > cout<<reslen<<e ndl;
            > }
            >
            > string Convert(int num)
            > {
            > stringstream test;
            > test << num;
            >
            > return test.str();
            > }
            >
            >[/color]


            Comment

            • BobR

              #7
              Re: string variable


              cdg wrote in message ...[color=blue]
              > 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 (){[/color]

              int main(){ // ALWAYS returns 'int'
              [color=blue]
              > int num = 123456789;
              > int tmlen(0);[/color]
              // > string result;
              // > result = Convert(num).c_ str();

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

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

              size_t reslen( result.size() );
              [color=blue]
              > cout<<reslen<<e ndl;[/color]

              return 0;[color=blue]
              >}
              >
              >string Convert(int num){
              > stringstream test;
              > test << num;
              > return test.str();
              >}[/color]

              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.):


              Then off to:
              ACCU - professionalism in programming


              --
              Bob R
              POVrookie


              Comment

              • Peter_Julian

                #8
                Re: string variable


                "cdg" <anyone@anywher e.com> wrote in message
                news:EspPf.1042 6$Tf3.5711@duke read09...
                | 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<<e ndl;
                | }
                |
                | 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::ostringstr eam 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>(67 89);
                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
                */

                Comment

                • benben

                  #9
                  Re: string variable

                  > Big hint.... What does std::string::c_ str() return? That should give[color=blue]
                  > you an idea as to the variable type.[/color]

                  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

                  Comment

                  Working...