Link to home
Start Free TrialLog in
Avatar of samford
samford

asked on

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];
     }



     
     
}
ASKER CERTIFIED SOLUTION
Avatar of Scotchman
Scotchman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of subhadeepin
subhadeepin

 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]);





   
   
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.

Avatar of samford

ASKER

Thanks Scotchman!  That is exactly what I needed to know.