I need some direction on how to split up code into a .h header and two .cpp files.

#include <iostream>
#include <cstring>
using namespace std;
 
//function for displaying name
void DisplayName(char * Name){
      cout<<"Name: "<<Name<<endl;         //print name on screen
}
 
int main(){
      char first_name[50];                      //static array for holding first name
      char middle_name[50];                     //static array for holding middle name
      char last_name[50];                             //static array for holding last name
 
      char choice;                                    //holds user choice
 
      int len;                                        //length of full name
 
      char *name;                                     //holds full name
      do{                                                   //loop
            cout<<"Please enter last name: ";   //prompt for last name
            cin>>last_name;                                 //read in last name
            cout<<"First name: ";                     //prompt for first name
            cin>>first_name;                          //read in first name
            cout<<"Middle name: ";                    //prompt for middle name
            cin>>middle_name;                         //read in middle name
 
            //compute length for entire name
            len=strlen(first_name)+strlen(middle_name)+strlen(last_name)+3;         //3 extra for spaces and terminating null character
 
            name=new char[len];                       //allocate memory
            if(name==NULL){                           //validating allocation
                  cout<<"Memory allocation error. Terminating."<<endl;  //allocation error
                  break;                                    //terminate loop
            }     //if
 
            strcpy(name,first_name);            //copy first name
            strcat(name," ");                   //append a space
            strcat(name,middle_name);           //append middle name
            strcat(name," ");                   //append a space
            strcat(name,last_name);             //append last name
 
            DisplayName(name);//invokes function to display name
 
            delete [] name;   //unallocate memory
 
            cout<<"Do you want to enter another name(y/n): ";     //ask if he/she want to continue
            cin>>choice;      //read in choice
            while(choice!='y' && choice!='Y' && choice!='n' && choice!='N'){  //validating choice
                  cout<<"Invalid choice. Please reenter(y/n): ";              //invalid choice. ask to reenter
                  cin>>choice;                        //read in choice again
            }     //while
 
      }while(choice=='y'||choice=='Y');         //loop as long as user wants
 
      return 0;
}

whats the point? The code is not complex enough to make a difference...

If this is your program:

#include <iostream>

void myFunction() {
   // ...
}

int main() {
   // ...
}

You need a prototype for the function:
myFunction.h

void myFunction();

Then you should put the function in its own .cpp file:
myFunction.cpp

#include "myFunction.h" // just to be safe

void myFunction() {
   // ...
}

Finally, what's left is the main function:
main.cpp

#include <iostream>
#include "myFunction.h" // this is for our function prototype!

int main() {
   // ...
}

That will work fine in some cases, but if you use the STL, for example, in the functions, you will need to add #includes to the .cpp file, or the .h file if necessary:
myFunction.cpp

// you might have to move this to the .h file
// if any STL objects are used in the function arguments
#include <iostream>
using namespace std;

void myFunction() {
    // ...
 }

[edit] A minor detail I forgot, you will need to make several more functions out of the code that is contained in main() before you can split it up.

>whats the point? The code is not complex enough to make a difference...
It could be a homework assignment. Most teachers won't force a student to learn splitting up code into several files by practicing on a 10,000 line project.

if it was a homework assignment. Wouldn't he use functions....

He did.

oh sorry missed the top bit.

Yep, it's homework. Thanks guys I'll give it shot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.