// name-printer.cpp : Defines the entry point for the console application.
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main ()
	string inputNameString; //the Name string that is entered by the user
	string names[3]; // A string array that will hold the first name , the middle name(if present) along with the last name

	//Prompt the User to Enter the name along with instructions on the formatting to be followed
	cout<<endl<<"Pleae Enter a name in the traditional western format with the following format"<<endl;
	cout<<"First Nname first and the Last name last perhaps with a  middle name, middle initial or neither"<<endl;
	cout<<"Use the white space to indicate a separation betweent the first name,  the middle name (if present) and the last Name "<<endl;

	//Read the input string from the console usng the getline and indicating that the input is to be read untill the enter key is pressed

	//process the input string to print the name in the format : lastname,firstname and witht the middle name replaced by an initial if present
    //Accomplish this by tokenizing the string using the str tok function

   char *  cstr = new char [inputNameString.size()+1]; //create a dynamic array of chars having same size as the inputted string
   strcpy (cstr, inputNameString.c_str()); //convert the string  to a cstyle string using the c_str() function since the str tok will work only on c style string
   char* tok = strtok (cstr, " "); //use the white space as delimiter

   int nameCount = 0;  //A variabel that holds index of the name array
   while (tok!=NULL) //loop untill no more tokens are found.
       if(nameCount<3) //A name can have first name, last name and or middle name only

		   names[nameCount++] = tok;
	    tok = strtok (NULL, " "); //continue Tokenizing


	string outputString; //the output string that will display the name in the format of lastname,firstname ,firstletterofmiddlename
	//determien if the user has entered a middle name or not
	if(nameCount==2) //the user has not entered a middle name
      outputString = names[1] + ", " + names[0]; // //here the lastname will be at the 1st position in the array ,the firstname at the 0th position
	else if (nameCount==3)
      string middleName = names[1];
	  char c = middleName[0];
	  outputString= names[2]+", "+names[0] + " "+ c +"."; //here the lastname will be at the 2nd position in the array , the middle name in the 1st pos and the firstname at the 0th position
	return 0;

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them

Major Fail!

1) No CODE Tags so the code is unreadable
2) No explanation of what the code is supposed to do
3) No explanation of what you're having trouble with

Okay sorry I fixed it now. Now how come I can't get the program to run?

Okay sorry I fixed it now. Now how come I can't get the program to run?

I fixed #1. #2 and #3 are still unknowns.

This article has been dead for over six months. Start a new discussion instead.