reverseName function

I am struggling with my program. My reverseName function does work properly, and i can't seem to be able to figure out why. I was wondering if i could get some help with it and possibly anything else that may be wrong with it.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int getPiece (char deliminator, char source []);
int pieceCount(char s[], char deliminator);
void reverseName (char inName[], char outName[]);

int main()
{
	
	ifstream infile;
	ofstream outfile;
	char filename[56];
	char fileout [56];
	char space = ' ';
	int parts;

	cout << "Enter the input file to be opened: ";
	cin.getline(filename, 255);
	cout << endl;

	cout << "Enter the name of the output file: ";
	cin.getline(fileout, 255);
	cout << endl;

	infile.open(filename);
	if(infile.fail())
	{
		cout << "Error: could not open " << filename << endl;
		exit (EXIT_FAILURE);
	}

	outfile.open(fileout);
	if(outfile.fail())
	{
		cout << "Error: could not open " << fileout << endl;
		exit (EXIT_FAILURE);
	}
	
	char name[56];
	char outName[56];

	infile.getline(name,55);
	
	while(!infile.eof())
	{
		
		parts = pieceCount(name, ' ');
		if (parts == 2)
		{
			reverseName(name, outName);
			outfile << "Names are: " << outName; // can't get anything to output!
	
		}
		else{ 
			reverseName(name, outName);
			getPiece(space, name);
			outfile << outName << ", " << name << endl;
			outfile << outName << endl;
		}

	infile.getline(name,60);
	}



	infile.close();
	outfile.close();


	system("Pause");
	return 0;
}



//-----------------------------------------------------------------------------
//getPiece -- Supposed to get middle name from a name and make it an initial
//-----------------------------------------------------------------------------
int getPiece (char deliminator, char s[])
{
	int i;
	for(i=0; s[i] != deliminator ;i++)
			s[i] = s[i];
	return i;
}

//-----------------------------------------------------------------------------
//pieceCount -- Counts how many names there are and if there is a middle name
//-----------------------------------------------------------------------------

int pieceCount(char s[], char deliminator)
{
	int count = 0;
	for (int i = 0; s[i] != '\0'; i++)
	{
		if (s[i] == ' ')
		{
			count ++;
		}
	}

	return count + 1;
}

//-----------------------------------------------------------------------------
// reverseName function
//-----------------------------------------------------------------------------


void reverseName (char  inName[], char outName[])
{
	char last[40];
	char first [40];
	char comma [3];

	comma[0] = ',';
	comma[1] = ' ';
	comma[2] = '\0';

	int x = 0;
	int i;
	for (i= getPiece(' ',inName); i < strlen(inName); i++){
		last[x] = inName[i];
		x++;
	}
	for (i=0; i < getPiece(' ', inName); i++){
		first[i] =inName [i];
	}
	first[i]= '\0';

	strcat(last, comma);

	strcat(last, first);
	strcpy(outName, last);


	return;	
}




Are you allowed to use the string class?

1
2
3
4
5
6
7
8
string sFirstName = "";
int iSpace = -1;

iSpace = FullName.find_first_of(" ");
sFirstName = FullName.substr(0, iSpace);

// etc
sReversedName = sLastName + "," + sFirstName;


If someone's full name is more than 40 chars, your program will have a buffer overflow. This is because you are cat'ing comma and first into your last name buffer which is sized to only 40 chars.

Last edited on
It crashes.
1
2
3
4
5
6
7
int getPiece (char deliminator, char s[])
{
  int i;
  for(i=0; s[i] != deliminator ;i++)
      s[i] = s[i];
  return i;
}


Thats a weird piece of code. It's to get the location correct?

1
2
3
4
5
6
7
8
int getPiece (char deliminator, char s[])
{
  for(int i=0; i < strlen(deliminator) ;i++)
    if (s[i] == deliminator)
      return i;

  return -1;
}
Last edited on
Now i get this error.... in the for loop
error C2664: 'strlen' : cannot convert parameter 1 from 'const char' to 'const char *'
closed account (z05DSL3A)
Can I just ask what the problem was with your original code? ie Why do you say it is not working properly.

I have just done a simple test and apart from putting a space at the start, when you give it somthing like "Grey Wolf" it returns " Wolf, Grey".

Hereis your code with a few minor mods:
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
void reverseName (char inName[], char outName[])
{
	char last[40] = {'\0'};
	char first [40] = {'\0'};
	char comma [3] = {'\0'};

	comma[0] = ',';
	comma[1] = ' ';
	comma[2] = '\0';

	int x = 0;
	int i;
	for (i= getPiece(' ',inName)+1; i < strlen(inName); i++) //+1 to getPiece to go to the
	{                                                        //next char aft the spacee
		last[x] = inName[i];
		x++;
	}
	for (i=0; i < getPiece(' ', inName); i++)
	{
		first[i] =inName [i];
	}
	first[i]= '\0';

	strcpy(outName, last);
	strcat(outName, comma);
	strcat(outName, first);

	return;	
}
Last edited on
I don't know it just kept crashing when i tried to run it. It was with that same function. I could comment it out and it would work. Now i just need to fix that strlen error.
closed account (z05DSL3A)
This may be what is wanted:
1
2
3
4
5
6
7
8
int getPiece (char deliminator, char s[])
{
  for(int i=0; i < strlen(s) ;i++)
    if (s[i] == deliminator)
      return i;

  return -1;
}


Your original code had the potential to blast through the end of the array if the delimiter was not found.
Last edited on
OK that worked. Now i can't get it to output and abreviated middle name.
Topic archived. No new replies allowed.