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