Arrays and Strings

I am currently trying to build a simple program that will allow me to query a small database that will hold certain criteria.  However, I need to take the  character strings (make and model of car) held in two separate arrays and combine the two strings and place the new combined string in a new array. In addition, I need to actually reverse the strings and place a comma in between them. (i.e. string1 contained in array1[0] is "Nissan" and string2 contained in array2[0] is "Sentra" and the new string to be placed in array3[0] is "Sentra, Nissan".  I have thought about strcat, but I can't seem to make it work so I removed it from the code.  Here is my source code for the problem that I have so far.  I appreciate any help that anyone can give me!

#include <iostream.h>
#include <fstream.h>
#include <string.h>


int main()
{
     int workorder[50];                         //***Array of work order numbers***
     
     typedef char nameString[20];          //***Array of 20 strings containing car
                                                  //      first names***
     nameString mechanic[50];               //***Array of last names of mechanics***
     nameString firstName[50];               //***Array that contains 50 characters that
                                                  //   make up car first names***
     nameString lastName[50];                         //***Array that contains 50 characters that
                                                  //   make up car last names***
     nameString car[50];                         //***Array that contains the combined first
                                                  //   and last names
     int hold1;                                  
     int hold2;
     int hold3;
     int cnt=0;
     int i=0;
     int count;                                   //***Variable that holds the number of lines in the
                                                  //   in file***
     ifstream inFile;

     inFile.open("car.txt");
     if (!inFile)
          cout << "This program did not open";
     else cout << "This program did open" << endl;

     inFile >> count;
          cout << count << endl;
         
     for (i=0; i<count; i++)
     {
          inFile >> firstName[i] >> lastName[i] >> workorder[i] >> mechanic[i];
          cout << firstName[i] << endl << lastName[i] << endl << workorder[i] << endl << mechanic[i] << endl;
     }    
     
     //**********************************************************
     //This is the part of the code that obviously isn't correct.
     //I have tried strcat, but I must not have done it properly,
     //so I changed the source code to reflect what I am trying to
     //do.
     //***********************************************************
     for (i=0; i < count; i++)                        
     {
          car[i] = lastName[i] + firstName[i];
     }



     
     
}
samfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ScotchmanCommented:
Maybe this will help.
I modified your code for my demo app. But I think you get the idea.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>


int main()
{
    int workorder[50];                         //***Array of work order numbers***

    typedef char nameString[20];          //***Array of 20 strings containing car
                                                 //      first names***
    nameString firstName[50]={"porsche","alfa","mercedes"};               //***Array that contains 50 characters that
                                                 //   make up car first names***
    nameString lastName[50]={"gt3","romeo","benz"};                         //***Array that contains 50 characters that
                                                 //   make up car last names***
    nameString car[50];                         //***Array that contains the combined first
                                                 //   and last names
    int counter;
    counter = 5;
    for (int i=0; i < counter; i++)
    {
         strcpy(car[i],firstName[i]);
               strcat(car[i]," , ");
         strcat(car[i],lastName[i]);
         cout << car[i] << endl;
    }
    getch();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
subhadeepinCommented:
 you cannot add two pointers thats what you are attemting in the following line===
   car[i] = lastName[i] + firstName[i];

Since you are able to read properly from the file and the only problem is storing properly in destination string in the reverse order ... the following code will do the work properly

strcpy(&destination_string[i][0],&source_string_2[i][0]);
strcat(&destination_string[i][0],",");
strcat(&destination_string[i][0],&source_string_1[i][0]);





   
   
0
MafaldaCommented:
1) Better use the ANSI C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


2) Better use the std::string !

string model[50];
string manufacturer[50];

3) use a simple operator+

string car[50];

for (i=0; i < count; ++i)                        
{
  car[i] = manufacturer[i] + "," +  model[i];
}

// ++i is better than i++ because it does'nt involve temp variables but the result is the same.

0
samfordAuthor Commented:
Thanks Scotchman!  That is exactly what I needed to know.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.