Output names from an input file

Here is my task....

Background

In this assignment, a person’s complete name has either two or three distinct parts, where each part is a string of non-space characters delimited by exactly one space. We will not use a name such as “Martin Van Buren” which has a space within the last name.

An input file contains a list of names, one name per line. The names are arranged in the first-name-first arrangement. For example:

John Fitzgerald Kennedy

Write a program that reads the names from this file and writes them to an output file in the last-name-first arrangement:

Kennedy, John F.

Put a comma and space between the last and first names, and limit the middle name to a correctly-formed initial. If the name only has two parts, such as:

Abraham Lincoln

Then omit the middle initial altogether:

Lincoln, Abraham

Let the user of your program enter the name of the input file and the name of the output file at run time. For example:


Select input file: names.txt
Select output file: newnames.txt



Implementation Details

Use C-strings for doing the character string work in this program. Use the standard C-string functions such as strlen, strcat, and strcpy as much as possible to minimize your need to do direct manipulations of the character array.

Include in your program a function that rearranges a name:

void reverseName(char inName[], char outName[]);

The input parameter is a name in the first-name-first order. The output parameter is the same name but in last-name-first order as described above.

You may write any other functions that you find helpful in organizing your program provided you design and implement them in keeping with good programming practice. Specifically, I suggest that you write and use the pieceCount() and getPiece() functions that we discussed in class. Exploit the concept of abstraction. Create generally useful tools that hide the details of char array manipulation.

Divide your program into multiple source-code files in the manner that we have studied in class. Use a macro guard to protect your header file from multiple includes. Also, include the usual box comments with your code: one at the beginning of each file and one before each function.


Sample Input File

This sample illustrates the input file. Your program must work correctly for an input file of any length. You may assume that each name is no more than 40 characters long, including the spaces between the parts.


Franklin Delano Roosevelt
Abraham Lincoln
William Henry Harrison
John Fitzgerald Kennedy
Rutherford Birchard Hayes
Thomas Jefferson
James Monroe
Thomas Woodrow Wilson
Warren Gamaliel Harding
John Quincy Adams
Franklin Pierce
Dwight David Eisenhower
Stephen Grover Cleveland
James Knox Polk



Sample Output File

Given the input file above, your program must produce this output file. Format the names as illustrated here.


Roosevelt, Franklin D.
Lincoln, Abraham
Harrison, William H.
Kennedy, John F.
Hayes, Rutherford B.
Jefferson, Thomas
Monroe, James
Wilson, Thomas W.
Harding, Warren G.
Adams, John Q.
Pierce, Franklin
Eisenhower, Dwight D.
Cleveland, Stephen G.
Polk, James K.



OK,now how do i write my functions? I am lost!
So far this is what i have but i dont know how to get to write to and output file, and correctly.
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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void reverseName (char inName[], char outName[]);

int main()
{
	
	ifstream infile;
	ofstream outfile;
	char filename[256];
	char fileout [256];
	

	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 oneLine[40];
	
	cin.getline(oneLine, 40);
	while(!infile.eof())
	{
		cin.getline(oneLine, 40);
		reverseName(filename, fileout);
	
	}	

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


	system("Pause");
	return 0;
}



//-----------------------------------------------------------------------------
// Function to change name
//-----------------------------------------------------------------------------

void reverseName (char inName[], char outName[])
{
	char firstname[40];
	char lastname [40];
	char last[40];
	char first [40];

	strcpy(firstname, last);
	strcpy(lastname, first);

	strcpy(firstname, lastname);

	strcat(lastname, last);
	strcat(firstname, first);
	
}


And don't know how to read just first initial of middle name
Last edited on
Anyone?
Hi....,
Inserting a small piece of code which may help U!!!

Use the same in ur program and test whether it fulfills ur reqt or not.

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
#include <stdio.h>

int formatName(char name[])
{
	const int  ch = ' ';
	char result[50] = "";
	char *spaceLoc = 0;

	int firstPos = 0;
	int lastPos = 0;

	/* Search for first space. */
	spaceLoc = strchr( name, ch );
	if(spaceLoc)
		firstPos = spaceLoc - name + 1;

	/* Search for last space. */
	spaceLoc = strrchr( name, ch );
	if(spaceLoc)
		lastPos = spaceLoc - name + 1;

	strcat(result,name+lastPos);
	strcat(result,", ");

	if(lastPos == firstPos)
	{
		strncat(result,name,firstPos-1);
	}
	else
	{
		strncat(result,name,firstPos+1);
	}

	printf("Name Before Formatting: %s\n",name);
	printf("Name After  Formatting: %s\n",result);

	return 0;
}


int main()
{
	char name1[] = "Franklin Delano Roosevelt";
	formatName(name1);

	char name2[] = "Abraham Lincoln";
	formatName(name2);

	return 0;
}



NB: Modification needed if the name does not contain any Space. :-). Hopefully U can handle the same.
ok so how would i get this to write to an output file instead of on the output screen?
You would use your stream like cout...i.e.:

1
2
outfile << stuff << endl;
//... 
So ur Prob got solved or still something persists ???
Ok i am a lot further than I was, however, in another hole. I can't get my getPiece function to work properly, and some errors that i don't know how to fix.

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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

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

int main()
{
	
	ifstream infile;
	ofstream outfile;
	char filename[256];
	char fileout [256];
	const int space = ' ';
	char 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[256];
	char outName[256];

	infile.getline(name,255);
	
	while(!infile.eof())
	{
		parts = pieceCount(name, 255); // error: argument' : truncation from 'const int' to 'char' & argument' : truncation of constant value
		if (parts == 2)
		{
			reverseName(name, outName);
			outfile << "Names are: " << outName; // can't get anything to output!
			infile.getline(name,40);
		}
		else 
			getPiece(name, ' ', parts); // error: 'getPiece' : cannot convert parameter 2 from 'const char' to 'char []'

	
	}



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


	system("Pause");
	return 0;
}



//-----------------------------------------------------------------------------
// getPiece
// Supposed to get the middle name of a list of names, output
// it but as an initial.
//-----------------------------------------------------------------------------
int getPiece (char result[], char s[], char deliminator)
{
	for(int i=0;s[i]!=' ';i++);
	   

return s[i];
}

//-----------------------------------------------------------------------------
// pieceCount
//-----------------------------------------------------------------------------

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

	return count + 1;
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------


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

	strcpy(inName, first);
	strcpy(inName, last);

	strcpy(inName, outName);

	strcat(inName, ", ");
	strcat(inName, first);
	strcat(inName, " ");
	outName = strcat(inName, last);

	return;	
}




???
Topic archived. No new replies allowed.