Menu

appending two strings

kip2847
2007-10-29
2012-09-26
  • kip2847

    kip2847 - 2007-10-29

    I need to add a string to another to form a sentence of some sort
    i.e.
    firststring + second string + third string
    ====================
    How + are + you

    so far here is my pusedo code:

    char article = {"the", "a", "one", "some","any"};
    char
    noun = {"boy", "girl", "dog", "town", "car"};
    char verb = {"drove", "jumped", "ran", "walked","skipped"};
    char
    preposition ={"to", "from", "over", "under", "on"};

    // function will only take char so i copy into another point (easy for me to read)
    char first = article[3];
    *char sec = noun[2]l

    //then i use the string function
    strcat(first, sec);

    at this point the program shuts down- not the type of error that gives me a log but the type that runs and then a win32 error occurs. from my understanding i am writing over other data bc of the use of the pointers... so how do i solve the problem.. i was thinking of creating aother char big enough to store the other smaller char but that seem like it didn't work same error occured .. any ideas will be apprciated

     
    • Anonymous

      Anonymous - 2007-10-29

      All your strings are constants (but you have a non-const pointer to them!), strcat() modifies the string in its first parameter, you cannot modify a constant. Another problem with strcat() is that the first string must have sufficient reserve space allocated to accommodate the concatenated string.

      Furthermore this code is not real:

      char first = article[3];
      char sec = noun[2]l

      for example is nonsense. And you cannot use an array initialiser on a simple pointer. This code won't compile. Post the real code if you want a definitive answer!

      The simplest solution is to simply use C++ std::string objects:

      include <string>

      include <iostream>

      include <cstdlib>

      using std::string ;
      using std::cout ;
      using std::endl ;

      int main()
      {

      const char article[] = {"the", "a", "one", "some","any"};
      const char
      noun[] = {"boy", "girl", "dog", "town", "car"};
      const char verb[] = {"drove", "jumped", "ran", "walked","skipped"};
      const char
      preposition[] ={"to", "from", "over", "under", "on"};

      string str = string(article[3]) + " " + string(noun[2]) ;
      cout << str << endl ;;

      system( "pause" ) ;
      }

      Clifford

       
    • kip2847

      kip2847 - 2007-10-29

      btw am using dev c++ 4.9.2
      windows xp

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.