Strings inside arrays

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

    Strings inside arrays

    Hi,
    First, Sorry if the title was bad, I really didn't know how to describe
    this.
    So,
    What I need to do is something like this:
    char somevariable[256]; // The tricky part. I don't know how to define
    it.
    somevariable[1] = "Hello";
    somevariable[2] = "Goodbye";
    And to print them using:
    printf(somevari able[1]);
    and
    printf(somevari able[2]);

    Hope I was describing this enough.
    Thanks, LinuxDuud.

  • Mike Wahler

    #2
    Re: Strings inside arrays


    "LinuxDuud" <ng1200@gmail.c omwrote in message
    news:1168970189 .421941.107920@ 51g2000cwl.goog legroups.com...
    Hi,
    First, Sorry if the title was bad, I really didn't know how to describe
    this.
    So,
    What I need to do is something like this:
    char somevariable[256]; // The tricky part. I don't know how to define
    it.
    somevariable[1] = "Hello";
    somevariable[2] = "Goodbye";
    And to print them using:
    printf(somevari able[1]);
    and
    printf(somevari able[2]);
    >
    Hope I was describing this enough.
    Thanks, LinuxDuud.
    #include <iostream>
    #include <string>
    #include <vector>

    using std::cout;
    using std::string;
    using std::vector;

    int main()
    {
    vector<stringte xt;

    text.push_back( "Hello");
    text.push_back( "The C++ standard library has many easy to use,");
    text.push_back( "robust and versatile types and algorithms. Use them.");
    text.push_back( "Goodbye");

    for(vector<stri ng>::size_type i = 0; i < text.size(); ++i)
    cout << text[i] << '\n';

    return 0;
    }

    -Mike


    Comment

    • Jack Klein

      #3
      Re: Strings inside arrays

      On 16 Jan 2007 09:56:29 -0800, "LinuxDuud" <ng1200@gmail.c omwrote in
      comp.lang.c++:
      Hi,
      First, Sorry if the title was bad, I really didn't know how to describe
      this.
      So,
      What I need to do is something like this:
      char somevariable[256]; // The tricky part. I don't know how to define
      it.
      somevariable[1] = "Hello";
      somevariable[2] = "Goodbye";
      And to print them using:
      printf(somevari able[1]);
      and
      printf(somevari able[2]);
      >
      Hope I was describing this enough.
      Thanks, LinuxDuud.
      Somebody else already showed you a way to use C++ vectors and streams
      to do what you want, completely ignoring the fact that you might have
      a good reason to use C style strings and the printf() function. And
      you should prefer vectors unless there is some reason that you cannot.

      But to answer your original question:

      const char *somevariable [256] =
      {
      "Hello",
      "Goodbye",
      /* up to 254 more string literals */
      };

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Noah Roberts

        #4
        Re: Strings inside arrays


        Mike Wahler wrote:
        "LinuxDuud" <ng1200@gmail.c omwrote in message
        news:1168970189 .421941.107920@ 51g2000cwl.goog legroups.com...
        Hi,
        First, Sorry if the title was bad, I really didn't know how to describe
        this.
        So,
        What I need to do is something like this:
        char somevariable[256]; // The tricky part. I don't know how to define
        it.
        somevariable[1] = "Hello";
        somevariable[2] = "Goodbye";
        And to print them using:
        printf(somevari able[1]);
        and
        printf(somevari able[2]);

        Hope I was describing this enough.
        Thanks, LinuxDuud.
        >
        #include <iostream>
        #include <string>
        #include <vector>
        >
        using std::cout;
        using std::string;
        using std::vector;
        >
        int main()
        {
        vector<stringte xt;
        >
        text.push_back( "Hello");
        text.push_back( "The C++ standard library has many easy to use,");
        text.push_back( "robust and versatile types and algorithms. Use them.");
        text.push_back( "Goodbye");
        >
        for(vector<stri ng>::size_type i = 0; i < text.size(); ++i)
        cout << text[i] << '\n';
        With such a message I would think your loop might look more like this:

        std::copy(text. begin(), text.end(),
        std::ostream_it erator<std::str ing>(cout, "\n"));
        >
        return 0;
        }
        >
        -Mike

        Comment

        • Mike Wahler

          #5
          Re: Strings inside arrays


          "Noah Roberts" <roberts.noah@g mail.comwrote in message
          news:1168972950 .084890.242350@ 11g2000cwr.goog legroups.com...
          >
          Mike Wahler wrote:
          >>
          > for(vector<stri ng>::size_type i = 0; i < text.size(); ++i)
          > cout << text[i] << '\n';
          >
          With such a message I would think your loop might look more like this:
          >
          std::copy(text. begin(), text.end(),
          std::ostream_it erator<std::str ing>(cout, "\n"));
          :-)
          Actually, I indeed almost did exactly that.

          But I thought better of it, since OP seems to obviously
          be a C++ beginner. I Didn't want to overload (pun intended)
          him with too much info right away.

          -Mike


          Comment

          • Grizlyk

            #6
            Re: Strings inside arrays


            Jack Klein wrote:
            Somebody else already showed you a way to use C++ vectors and streams
            to do what you want, completely ignoring the fact that you might have
            a good reason to use C style strings and the printf() function. And
            you should prefer vectors unless there is some reason that you cannot.
            I think so, that printf() can be used in some cases if you well know
            the function, but you can get wrong output, if you will mix
            "std::stdou t" and "std::cout" streams. To avoid backside effects use
            "printf" and "<<" into separate streams, for example, do printf into
            stderror or file and << into cout.

            Comment

            • Lionel B

              #7
              Re: Strings inside arrays

              On Tue, 16 Jan 2007 21:19:35 -0800, Grizlyk wrote:
              Jack Klein wrote:
              >
              >Somebody else already showed you a way to use C++ vectors and streams
              >to do what you want, completely ignoring the fact that you might have
              >a good reason to use C style strings and the printf() function. And
              >you should prefer vectors unless there is some reason that you cannot.
              >
              I think so, that printf() can be used in some cases if you well know
              the function, but you can get wrong output, if you will mix
              "std::stdou t" and "std::cout" streams. To avoid backside effects
              ....please adjust your diet ;) Seriously though, not quite sure what you
              intended there.
              use "printf" and "<<" into separate streams, for example, do printf into
              stderror or file and << into cout.
              Is that necessary? I think a call to std::ios_base:: sync_with_stdio (true)
              should synchronise the standard C I/O facilities (stdout, etc.) with their
              standard C++ equivalents (std::cout, etc.).

              (Indeed it used to be [still is?] recommended that if you are using one or
              the other of C or C++ I/O facilities then it might be a good idea to call
              sync_with_stdio (false), since synching I/O modes might entail a substantial
              speed/size overhead).

              --
              Lionel B

              Comment

              • Old Wolf

                #8
                Re: Strings inside arrays

                Lionel B wrote:
                Is that necessary? I think a call to std::ios_base:: sync_with_stdio (true)
                should synchronise the standard C I/O facilities (stdout, etc.) with their
                standard C++ equivalents (std::cout, etc.).
                They are sync'd by default; you would only call that function if you
                wanted to unsync them.

                Comment

                • Lionel B

                  #9
                  Re: Strings inside arrays

                  On Wed, 17 Jan 2007 14:12:29 -0800, Old Wolf wrote:
                  Lionel B wrote:
                  >Is that necessary? I think a call to std::ios_base:: sync_with_stdio (true)
                  >should synchronise the standard C I/O facilities (stdout, etc.) with their
                  >standard C++ equivalents (std::cout, etc.).
                  Yep, sorry, that's the wrong way round...
                  They are sync'd by default; you would only call that function if you
                  wanted to unsync them.
                  For that reason it was often recommended that you explicitly *un*-sync them
                  with std::ios_base:: sync_with_stdio (false) for better I/O performance if
                  you were not intending to mix C and C++ style I/O in the same program.

                  --
                  Lionel B

                  Comment

                  Working...