Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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



     
     
}
0
samford
Asked:
samford
1 Solution
 
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
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now