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;
}
|