Thread: concatenation

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    concatenation

    I am scanning names from a text file and placing the last name in one array and the first name in another. I need to concatenate the first name to the end of the last and place it in a new array.

    I have tried several different combinations of strcat &strcpy & no luck.

    One thing I have tried after placing the names in the arrays is

    for(i=0;i<max;++i)
    strcat(last[i],first[i]);
    strcpy(name[i],last[i]);

    Any suggestions?

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I did something similar a couple of months ago...maybe this will help even though it is not exactly what you are looking for
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    using namespace std;
    
    const int MAXNUM = 20;
    
    int main()
    { 
              int nameCount = 0;
              char firstName[80];
              char middleName[80];
              char lastName[80];
              char names[20][83];
    
             //Get the names from "names.txt"
              ifstream inStream("names.txt"); //open the file
     
               if(inStream.fail()){
                         cout << "Input file opening failed.\n";
                         exit(1);
               }
    
              //main loop for processing names line by line
              for (int i=0; i < MAXNUM && !inStream.eof(); i++) {
                       inStream >> firstName >> middleName >> lastName;
    
                       names[i][0] = '\0'; // make sure it's an empty string
    
                       strcat(names[i], lastName); //get lastName first
                       strcat(names[i], ", ");
    
                       strncat(names[i], middleName, 1); //get middleName initial
                       strcat(names[i], ". ");
    
                        if (strcmp(firstName, "BOB") == 0){ //get firstName at last
                                 strcat(names[i], "Robert");
                        }
                         else{
                                 for(int k=1; k< strlen(firstName); k++){
                                        firstName[k]=tolower(firstName[k]);
                                        strcat(names[i], firstName);
             }
    
             nameCount++; //record totally how many names are in the file
    }
    
            //Write the names in new format back to "names_newFormat.txt"
            ofstream outStream("names_newFormat.txt");
    
            if(outStream.fail()){
                    cout << "Output file opening failed.\n";
                    exit(1);
             }
    
              for (int j=0; j < nameCount-1; j++) //output the names in new format
                     outStream << names[j] << endl;
    
              inStream.close(); //close the file
              outStream.close();
    
              return 0;
    }
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Alipha
    Guest

    well, your method destroys the last name array

    you'll probably want to copy last into name array and then append the first onto it:

    for(i = 0; i < max; ++i) {
    strcpy(name[i], last[i]);
    strcat(name[i], first[i]);
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    That's one of the ones I tried and it didn't work.

  5. #5
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Do you really need to use cstring?
    cause you could do the same with c++ string like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
          string first = "George";
          string last = "Bush";
          string final = "";
          for(unsigned int n = 0; n<(last.length());n++)	final += last[n];
          final += ' ';	// insert space between names
          for(unsigned int n = 0; n<(first.length());n++)	final += first[n];
          cout << final << endl;
          return 0;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  6. #6
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    well here is the same using cstring
    (cstring arent c++ they are plain c..)
    but you can achieve the same..

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
          int max =4;
          char first[max][256]; strcpy(first[0],"George"); strcpy(first[1],"robert");  strcpy(first[2],"bob");  strcpy(first[3],"luigi");
          char last[max][256]; strcpy(last[0],"Bush"); strcpy(last[1],"barbeau"); strcpy(last[2], "floyd"); strcpy(last[3],"kenedy");
          char final[max][256];
          strcpy(final[0],"");	strcpy(final[1],"");	strcpy(final[2],"");	strcpy(final[3],"");
          
          for(int i =0; i<max;i++)		strcpy(final[i],last[i]);
          for(int i =0; i<max;i++)		strcat(final[i]," ");
          for(int i =0; i<max;i++)		strcat(final[i],first[i]);
          for(int i =0; i<max;i++)		cout << final[i] << endl;
          for(int i =0; i<max;i++)		cout << last[i] << endl;
          for(int i =0; i<max;i++)		cout << first[i] << endl;
          return 0;
    }
    Also use code tags if u want people to look at ur code..
    Last edited by Luigi; 04-25-2003 at 01:22 PM.
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    It's for a project and I can not use anything we have not covered in class so I have to use the above.

  8. #8
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    updated my last code might suits your needs better..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Thanks. I'll give it a try.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    my suspicion is that you aren't acquiring data like you think you are because the code for concatenation of last and first into name appears ok. Try printing each value read in to the screen after putting it in the appropriate index of first, last, hours.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    I did & it prints everything in the file correctly but then it adds a bunch if II's and recopies the first line of code and gives some big number.. so I know I have 2 problems and that may be why it won't concat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 06:47 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 06:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM