Monday, 28 May 2012

String Entering Example

This example shows how to perform data entry on strings. The strings are simple array of characters and they are declared as member variables of a class. The class provides accessory methods for each member variable.
Header File: Person.h
//---------------------------------------------------------------------------
#ifndef PersonH
#define PersonH

//---------------------------------------------------------------------------
class TPerson
{
private:
    char* FirstName;
    char* LastName;
    char* Address;
    char* City;
     char* State;
    long  ZIPCode;

public:
    void  setFirstName(const char *FN);
    char* getFirstName() const { return FirstName; }
    void  setLastName(const char *LN);
    char* getLastName() const { return LastName; }
    char* FullName() const;
    void  setAddress(const char *Adr);
    char* getAddress() const { return Address; }
    void  setCity(const char *CT);
    char* getCity() const { return City; }
    void  setState(const char *St);
    char* getState() const { return State; }
    void  setZIPCode(const long ZIP);
    long  getZIPCode() const { return ZIPCode; }

    TPerson();
    TPerson(char *FName, char *LName, char *Adr,
            char *Ct, char *St, long  ZIP);
    TPerson(const TPerson &Pers);
    TPerson(char * FName, char * LName);
    virtual ~TPerson();
};
//---------------------------------------------------------------------------
#endif
Source File: Person.cppfont>
//---------------------------------------------------------------------------

#i#include <iostream>
using namespace std;

#pragma hdrstop

#include "Person.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------
char *__fastcall TPerson::FullName() const
{
    char *FName = new char[40];
    strcpy(FName, FirstName);
    strcat(FName, " ");
    strcat(FName, LastName);

    return FName;
}
//---------------------------------------------------------------------------
__fastcall TPerson::TPerson()
    : ZIPCode(0)
{
    //TODO: Add your source code here  
    FirstName = new char[20];
    strcpy(FirstName, "John");
    LastName  = new char[20];
    strcpy(LastName, "Doe");
    Address   = new char[40];
    strcpy(Address, "123 Main Street Apt A");
    City      = new char[32];
    strcpy(City, "Great City");
    State     = new char[30];
    strcpy(State, "Our State");
}
//---------------------------------------------------------------------------
__fastcall TPerson::TPerson(char * FName, char * LName)
    : ZIPCode(0)
{
    //TODO: Add your source code here
    FirstName = new char[strlen(FName) + 1];
    strcpy(FirstName, FName);
    LastName  = new char[strlen(LName) + 1];
    strcpy(LastName, LName);
    Address   = new char[40];
    strcpy(Address, "123 Main Street Apt A");
    City      = new char[32];
    strcpy(City, "Great City");
    State     = new char[30];
    strcpy(State, "Our State");
} 
//---------------------------------------------------------------------------
__fastcall TPerson::TPerson(char *FName, char *LName, char *Adr,
                       char *Ct, char *St, long  ZIP)
    : ZIPCode(ZIP)
{                          
    //TODO: Add your source code here  
    FirstName = new char[strlen(FName) + 1];
    strcpy(FirstName, FName);
    LastName  = new char[strlen(LName) + 1];
    strcpy(LastName, LName);
    Address   = new char[40];
    strcpy(Address, Adr);
    City      = new char[32];
    strcpy(City, Ct);
    State     = new char[30];
    strcpy(State, St);
}
//---------------------------------------------------------------------------
__fastcall TPerson::TPerson(const TPerson &Pers)
    : ZIPCode(Pers.ZIPCode)
{              
    //TODO: Add your source code here
    FirstName = new char[strlen(Pers.FirstName) + 1];
    strcpy(FirstName, Pers.FirstName);
    LastName  = new char[strlen(Pers.LastName) + 1];
    strcpy(LastName, Pers.LastName);
    Address   = new char[strlen(Pers.Address) + 1];
    strcpy(Address, Pers.Address);
    City      = new char[strlen(Pers.City) + 1];
    strcpy(City, Pers.City);
    State     = new char[strlen(Pers.State) + 1];
    strcpy(State, Pers.State);
}
//---------------------------------------------------------------------------
void __fastcall TPerson::setFirstName(const char *FN)
{
    strcpy(FirstName, FN);
}
//---------------------------------------------------------------------------
void __fastcall TPerson::setLastName(const char *LN)
{
    strcpy(LastName, LN);
}
//---------------------------------------------------------------------------
void __fastcall TPerson::setAddress(const char *Adr)
{
    strcpy(Address, Adr);
}
//---------------------------------------------------------------------------
void  __fastcall TPerson::setCity(const char *CT)
{
    strcpy(City, CT);
}
//---------------------------------------------------------------------------
void  __fastcall TPerson::setState(const char *St)
{
    strcpy(State, St);
}
//---------------------------------------------------------------------------
void  __fastcall TPerson::setZIPCode(const long ZIP)
{
    ZIPCode = ZIP;
}
//---------------------------------------------------------------------------
__fastcall TPerson::~TPerson()
{
    //TODO: Add your source code here
    delete [] FirstName;
    delete [] LastName;
    delete [] Address;
    delete [] City;
    delete [] State;
}
//---------------------------------------------------------------------------
Main File: Main.cpp
////---------------------------------------------------------------------------
#include <iostream>
using namespace std;

#agma hdrstop

#i#include "Person.h"
//---------------------------------------------------------------------------

#pragma argsused

TPerson ProcessRegistration();
void DisplayInformation(const TPerson&);
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
    TPerson Employee = ProcessRegistration();
    DisplayInformation(Employee);

    return 0;
}
//---------------------------------------------------------------------------
TPerson ProcessRegistration()
{
    char FName[12], LName[12];
    char Addr[40], CT[32], St[30];
    long ZC;

    cout << "Enter personal information\n";
    cout << "First Name: "; cin >> FName;
    cout << "Last Name:  "; cin >> LName;
    cout << "Address:    "; cin >> ws;
    cin.getline(Addr, 40);
    cout << "City:       ";
    cin.getline(CT, 32);
    cout << "State:      ";
    cin.getline(St, 30);
    cout << "Zip Code:   "; cin >> ZC;

    TPerson Pers(FName, LName, Addr, CT, St, ZC);
    return Pers;
}
//---------------------------------------------------------------------------
void DisplayInformation(const TPerson& Pers)
{
    cout << "\nEmployee Identification";
    cout << "\nFull Name: " << Pers.FullName();
    cout << "\nAddress:   " << Pers.getAddress();
    cout << "\nCity:      " << Pers.getCity() << ", "
         << Pers.getState() << " " << Pers.getZIPCode();
}
//---------------------------------------------------------------------------

Here is an example of running the program:
EnEnter personal information
First Name: Jeremy
Last Name:  Nguyen
Address:    1466 16th Street #B
City:       WashingtonState:      DC
ZZip Code:   20004

Employee Identification
Full Name: Jeremy Nguyenddress:   1466 16th Street #B
City:      Washington, DC 20004

No comments:

Post a Comment