Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trouble building a dynamic string array. Coming from C#

Trouble building a dynamic string array. Coming from C#

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++data-structureshelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Randel
    wrote on last edited by
    #1

    I've already written my code in C# and it works. But to integrate it into our product, I have to re-write it C++ 6.0. I'm very weak here and am lost. My problem is this: I start with a line extracted from a file. The string contains a variable number of individual commands consisting of one letter followed by a number. In C# my code is built like this:

    private string[] LineSplitter(string LineIn){
    if (LineIn == "")
    return null;
    string[] strOut = new string[0];
    int i = 0;
    string strChar;
    do
    {
    ...code to locate length of number following letter
    } while ((isNumeric(strChar)) && (i + 1 < LineIn.Length));
    strOut = (string[])ResizeArray(strOut, strOut.Length + 1);
    strOut[strOut.Length - 1] = LineIn.Substring(0, i);
    LineIn = LineIn.Substring(i);
    } while (LineIn.Length > 0);
    return strOut;
    }

    (And I'm sorry for putting C# in a C++ group, but I'm looking for the C++ equivalent) Here are my questions... 1. How do I declare the function as string array? 2. How do I increment array elements until I'm done? Each line can have a variable number of elements. 3. How do I return the result. Please be gentle. I'm struggling with C++ and this is my first post. I'm wrestling through the majority of everything else in this process, but this has my brain in a pickle.

    _ 1 Reply Last reply
    0
    • M Mark Randel

      I've already written my code in C# and it works. But to integrate it into our product, I have to re-write it C++ 6.0. I'm very weak here and am lost. My problem is this: I start with a line extracted from a file. The string contains a variable number of individual commands consisting of one letter followed by a number. In C# my code is built like this:

      private string[] LineSplitter(string LineIn){
      if (LineIn == "")
      return null;
      string[] strOut = new string[0];
      int i = 0;
      string strChar;
      do
      {
      ...code to locate length of number following letter
      } while ((isNumeric(strChar)) && (i + 1 < LineIn.Length));
      strOut = (string[])ResizeArray(strOut, strOut.Length + 1);
      strOut[strOut.Length - 1] = LineIn.Substring(0, i);
      LineIn = LineIn.Substring(i);
      } while (LineIn.Length > 0);
      return strOut;
      }

      (And I'm sorry for putting C# in a C++ group, but I'm looking for the C++ equivalent) Here are my questions... 1. How do I declare the function as string array? 2. How do I increment array elements until I'm done? Each line can have a variable number of elements. 3. How do I return the result. Please be gentle. I'm struggling with C++ and this is my first post. I'm wrestling through the majority of everything else in this process, but this has my brain in a pickle.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Mark Randel wrote:

      1. How do I declare the function as string array?

      Not exactly an array but this is the C++ way -

      std::vectorstd::string

      Mark Randel wrote:

      How do I increment array elements until I'm done? Each line can have a variable number of elements.

      while (LineIn.size() > 0)

      Mark Randel wrote:

      How do I return the result.

      std::vectorstd::string strOut;
      .
      .
      .
      return strOut;

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      modified on Wednesday, November 18, 2009 5:53 PM

      M 1 Reply Last reply
      0
      • _ _Superman_

        Mark Randel wrote:

        1. How do I declare the function as string array?

        Not exactly an array but this is the C++ way -

        std::vectorstd::string

        Mark Randel wrote:

        How do I increment array elements until I'm done? Each line can have a variable number of elements.

        while (LineIn.size() > 0)

        Mark Randel wrote:

        How do I return the result.

        std::vectorstd::string strOut;
        .
        .
        .
        return strOut;

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        modified on Wednesday, November 18, 2009 5:53 PM

        M Offline
        M Offline
        Mark Randel
        wrote on last edited by
        #3

        Thanks a lot. The vector was the key to what I was looking for. I'm using CString, though. But it all works fine. My question about adding to the array was concerning changing the size of the array. The vector makes that easy with the push_back command. Also, returning the value from the function required that I make the type of the return as a vector:

        std::vector CNCLine::LineSplitter(CString LineIn)
        {
        std::vector strOut;
        ... code
        return strOut;
        }

        Works like clockwork. Thanks for the direction.

        ____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W

        M 1 Reply Last reply
        0
        • M Mark Randel

          Thanks a lot. The vector was the key to what I was looking for. I'm using CString, though. But it all works fine. My question about adding to the array was concerning changing the size of the array. The vector makes that easy with the push_back command. Also, returning the value from the function required that I make the type of the return as a vector:

          std::vector CNCLine::LineSplitter(CString LineIn)
          {
          std::vector strOut;
          ... code
          return strOut;
          }

          Works like clockwork. Thanks for the direction.

          ____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W

          M Offline
          M Offline
          Mark Randel
          wrote on last edited by
          #4

          Interesting... the post erased the "" command after the vector designation. Hope it doesn't with this text. Otherwise, I'm going to end up looking quite foolish. :D

          ____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W

          _ 1 Reply Last reply
          0
          • M Mark Randel

            Interesting... the post erased the "" command after the vector designation. Hope it doesn't with this text. Otherwise, I'm going to end up looking quite foolish. :D

            ____________________________________________________ I'd rather have a frontal lobotomy than a bottle in front of me... Bill W

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            That is because < and > are using by HTML for the tags. Replace < with &lt; and > with &gt; In fact you can simply click on the signs at the top of the editor to insert them.

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups