C string style problem

Hi, I'm not sure if any one will have the time to take a look at this and help me but I've been working for a couple hours a day on it for three days and I'm still stuck. First let me explain the program.
Name Arranger
Write a program that asks for the user's first, middle, and last names. The names should be stored in three different character arrays. The program should the store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. Ex. if the user entered "Carol Lynn Smith", it should store "Smith, Carol Lynn" in the fourth array. Display the contents of the fourth array to the screen.
Here is what I've got so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;
const int SIZE = 25;

void nameArranger(char *, char *, char *);

int main()
{
	cout << "Enter your first name: ";
	char firstName[SIZE];	
	cin >> firstName;
	int firstLength = strlen(firstName);

	firstName[firstLength] = ' ';//to store a space for later formatting
	firstName[firstLength + 1] = '\0';//to include the null terminator after replacing it with the space
	//for(int count = 0; count != firstLength; count++)
		//cout << firstName[count];

	cout << "Now your middle name: ";
	char middleName[SIZE];
	cin >> middleName;
	//cout << middleName << "\n";


	cout << "And finally your last name: ";
	char lastName[SIZE];
	cin >> lastName;
	int lastLength = strlen(lastName);
	
	lastName[lastLength] = 44;//44 is the ASCII code for a comma
	lastName[lastLength + 1] = ' ';//space for formatting with the comma
	lastName[lastLength + 2] = '\0';//reinclude the null terminator
	//for(int count = 0; count != (lastLength + 2); count++)
	//	cout << lastName[count];

	nameArranger(firstName, middleName, lastName);
}

void nameArranger(char *first, char *middle, char *last)
{
	char fullName[SIZE * 3];

	int length = (strlen(first) + strlen(middle) + strlen(last));

	strcat(first, middle);//append middle name to last
	

	for(int count = 0; count != length; count++)
	{
		fullName[count] = last[count];
		if(last[count] == '\0')
			do{
				fullName[count] = first[count - (strlen(last))];
				count++;
				
			} while(count != (length - 1));//Any number that is equal to length and over causes the program to crash 
											//yet length - 1 is not enough to display the full name
	}

	for(int count = 0; count != length; count++)
		cout << fullName[count];
}

And when I run this program with the name "Carol Lynn Smith" here is my output.
1
2
3
4
Enter your first name: Carol
Now your middle name: Lynn
And finally your last name: Smith
Smith, Carol Lynâ• Press any key to continue . . .

As you can see I am always one character short of displaying the full name. I feel like I am overwriting the array or something but I don't see how. I think I've explained and commented enough to help you understand what I'm asking, if not I can always provide more details. Please tell me if you see anything with this code that is wrong or could be improved. Thanks!
By the way, your comment is wrong in line 48.

But on to your problem.

You increment count in line 57, so at line 59, count equals the number of characters in the string, and is indexing fullName to the next available character. So, the do/while loop does not execute for the last character in the middle name. So you want to change 59 back to "!= length". Actually, "< length" would be better.

That brings us back to the original problem you have. You attempted to solve it the wrong way. The reason you had problems is when you dropped out of the do/while loop, count == length. When it hit the bottom of the for loop, count got incremented, so count == length + 1. The loop tried to execute forever until the program crashed.

The solution is to change your for loop condition to count < length.
Last edited on
Wow you must be really good at programming, even after explaining it I still don't understand what's wrong, but your way works! Hopefully I can figure it out.
I haven't analyzed it carefully to see exactly where your indices are getting messed up, but you should be able to see that the way you are playing with them are asking for trouble on lines 50-59.

A couple of comments to help you reconsider the way you have written it (for something simpler):

Lines 17-18, 33-25: Why are you doing this to your names? The assignment asks you to store the name, not the name + other characters.

(The problem is that you are trying to pre-process or optimize something that doesn't need it later.)

Line 33: Why are you using 44 instead of ','? If you want a comma, use a comma. [edit] This shouldn't matter if you fix things right. See below[/edit]

Line 48: You are modifying the firstName again. Don't do that. (It should only contain the first name, as per the instructions you've been given.)

Lines 51-61: Since you are already using strcat(), why are you not using it here?

1
2
3
4
5
fullName[0] = '\0';  // Important to make it a properly-terminated empty string

strcat( fullName, last );
strcat( fullName, ", " );
...

Lines 63-64: Why are you printing the name one letter at a time?

 
cout << fullName << endl;

Hope this helps.
Last edited on
Topic archived. No new replies allowed.