?
Solved

Arrays and Strings

Posted on 2003-02-27
4
Medium Priority
?
251 Views
Last Modified: 2010-04-01
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
Comment
Question by:samford
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Accepted Solution

by:
Scotchman earned 200 total points
ID: 8040001
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
 

Expert Comment

by:subhadeepin
ID: 8040115
 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
 
LVL 6

Expert Comment

by:Mafalda
ID: 8040689
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
 

Author Comment

by:samford
ID: 8046017
Thanks Scotchman!  That is exactly what I needed to know.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question