this program reads in a First name, middle name or initial, last name and then outputs the last name, first name then middle initial. The problem is when you don’t give a middle name or initial then the program output Is not correct. Look below for the output. The output should just be User, Mary when middle initail or name is not given.

Please be specific as to where i need to make the adjustment.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

int const MAX_NAME = 20 ;
using namespace std;

int main()
{
	char fName [MAX_NAME + 1]; 
	char mOrInitial [MAX_NAME + 1]; 
	char lName [MAX_NAME + 1];
	
	char temp1 [MAX_NAME + 1]; 
	char temp2 [MAX_NAME + 1];

	


	cout << "Enter first name, then middle name or initial, and then last name: " << endl ;

	char szBuff [80] ;
	cin.getline(szBuff, 80, '\n') ; 

	char* p = szBuff ; // get first name
	char* p1 = fName ;
	while ((*p1++ = *p++) != ' ' && *p != '\0') ;
	*p1 = '\0' ;
	
	while (*p == ' ' && *p != '\0') p++ ;

	p1 = temp1 ;
	while ((*p1++ = *p++) != ' ' && *p != '\0') ;
	*p1 = '\0' ;
	
	while (*p == ' ' && *p != '\0') p++ ;

	if (*p) 
	{
		p1 = temp2 ;
		while ((*p1++ = *p++) != ' ' && *p != '\0') ;
		*p1 = '\0' ;
	}

	if ('\0' == temp2 [0]) // no middle name was given
	{
		strcpy (lName, temp1) ;
	}
	else 
	{
		strcpy (mOrInitial, temp1) ;
		strcpy (lName, temp2) ;
	}

	// Last_Name, First_Name Middle_Initial. 

	if ('\0' == mOrInitial[0]) 
	{
		printf (lName) ;  printf (", ") ;
		printf (fName) ;
	}
	else 
	{
		printf (lName) ; printf (", ") ;
		printf (fName) ; printf (" ") ;
		printf ("%c.", mOrInitial[0]) ;
	}

	printf ("\n") ;

	_getch () ;

	return 0;
}
//Right format with middle name given
//Enter first name, then middle name or initial, and then last name:
//Mary Average User
//User, Mary  A.
//Press any key to continue . . .


//Not right without giving middle name. Output should be just User, Mary.
//Enter first name, then middle name or initial, and then last name:
//Mary User
//╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠User, Mary  U.

Why are you mixing C and C++ style I/O? As a general rule that is not routinely recommended.

Use a conditional statement to print the middle name. If the middle name is empty then don't print it.

Option 1:
If the middle name is declared as an object of STL string class then empty() is a class method you can use.

Option 2:
If middle name is a C style string, then you can use middleName[0] = '\0'; as a default value to make it empty. Then check the first element of middleName to see if it is something other than the null char before printing it out.

Another way to do this is have the whole name entered on one line than parse through the string and pull the names out of it. A good way would be to see how many space are in the string. if there are 2 spaces than you know you have a middle name or initial. If there is only one space than you only have a first and last name.

Unless of course the name is something like:

James St. John

or

Jack Smith, Jr

or

George Bush, III

But, I admit, that's a little persnickety.

yes that does break what i said but than again he didn't say anything about prefixes or suffixes in his post. but that was a very good point out ;)

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.