// 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
getline(cin,inputNameString,'\n');
//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;
}
else
{
break;
}
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
cout<<outputString<<endl;
}
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
cout<<outputString<<endl;
}
cin.get();
return 0;
}
byrosport
0
Newbie Poster
Recommended Answers
Jump to PostMajor 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
Jump to PostYou need to include the <cstring> header
All 5 Replies
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
byrosport
0
Newbie Poster
sfuo
111
Practically a Master Poster
byrosport
0
Newbie Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
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.