I've been trying for hours and still haven't been able to figure out whats the problem. I've tried many variations in the functions, but still unsure. I'm trying to make a program that changes the inputted array of names first name middle name, and last name to different format (Last Name, First Name Middle Inital). While capitalizing the first letters and making the middle name an inital. I'm trying to get the first, middle, and last names into one array first which are seperated by spaces, then copying them into different arrays, character by character using a for loop. Then deleting the name copied in the main array by just moving the arrays back to array 0 from the first space. I'm thinking there might be problems with the two functions I made to send the names to the other arrays and the array moving back portion. Please bare with my coding, still a beginner... anyone have any advice on what might be the problem?

#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

void sort_name(char name[], char new_name[]);
void remove_name(char name[], int space);

const int NUM_LETTERS = 21;
const int MAX_LETTERS = 64;

int main()
{
  char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS],
       middle_inital[NUM_LETTERS];

  cout << "\nPlease enter your first name, middle initial, and last name.\n"
       << "(max 20 letters per name)\n";
  cin >> get_name;

  sort_name(get_name, first_name);
  sort_name(get_name, middle_inital);
  sort_name(get_name, last_name);

  cout << last_name << ", " << first_name << " " <<  middle_inital << "." << endl;

  return 0;
}

void remove_name(char name[], int space)
{
  int size = NUM_LETTERS;
//  space++;

  for(int i = space; i < space; i--)
    name[i] = name[i+1];
}

void sort_name(char name[], char new_name[])
{
  for(int i=0; i<MAX_LETTERS; i++)
  {
    if(!isspace(name[i]))
    {
      remove_name(name, i);
      break;
    }
    else
      new_name[i] = name[i];
  }
 new_name[0] = static_cast<char>(toupper(new_name[0]));
}

Recommended Answers

All 4 Replies

Greetings.
Hi. Could you please provide a set of input and its expected output?
Just to make sure I understood perfectly and would be analysing your code correctly. :)

The output I'm getting is

Please enter your first name, middle initial, and last name.
(max 20 letters per name)
william jefferson clinton
William, William William.

but the expected output is,

Please enter your first name, middle initial, and last name.
(max 20 letters per name)
william jefferson clinton
Clinton, William J.

Greetings.
Thanks.
I've modified a bit, it may not be as efficient. Hope this helps ;)

#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

void test(char [], int, char[]);

const int NUM_LETTERS = 21;
const int MAX_LETTERS = 64;

char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS];
char middle[NUM_LETTERS];
int until;

int main()
{
  cout << "\nPlease enter your first name, middle initial, and last name.\n"
       << "(max 20 letters per name)\n";
  cin.getline( get_name, MAX_LETTERS);

  test(get_name, strlen(get_name), last_name);
  test(get_name, until, middle);
  test(get_name, until, first_name);
  
  cout<<last_name<<", "<<first_name<<" "<<middle[0]<<"."<<endl;
  return 0;
}

void test(char name[], int i, char new_name[NUM_LETTERS])
{
	int size=i;
	int j=0;

	while(!isspace(name[i]) && i>=0)
	{
		i-=1;
	}
	until = i-1;
	for( int x=i+1; x<=size; x++)
	{
		new_name[j] = name[x];
		j+=1;
	}
	new_name[0] = static_cast<char>(toupper(new_name[0]));
}

How about:

#include <iostream.h>
#include <ctype.h>
#include <conio.h>
void main()
{
  cout<<"please enter first name, middle name and last name"<<endl;
  cout<<"(max 20 chars each)"<<endl;
  char fn[20],mn[20],ln[20];
  cin>>fn>>mn>>ln;
  fn[0]=toupper(fn[0]);
  mn[0]=toupper(mn[0]);
  ln[0]=toupper(ln[0]);
  cout<<ln<<", "<<fn<<" "<<mn[0]<<".";
}

:)

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.