#include <iostream>
#include <iomanip> // set width and decimal
#include <fstream> // use file input and output
#include <cstring> // use strings
using namespace std;
ifstream infile;
ofstream outfile;
string input(string, int);
string copy(string, string, int);
string breakup(string, string, string, string, int);
string capt(string, string, string, string, int);
string sort(string, int);
string print(string, int);
string input(string a[], int &cnt)
{
for (int i = 0; !infile.eof(); i++)
{
getline(infile, a[i]);
cnt = i;
}
}
string copy(string a[], string b[], int cnt)
{
for (int i = 0; i < cnt; i++)
{
b[i] = a[i];
}
}
string breakup(string a[], string first, string middle, string last, int cnt)
{
int lastnum = 0;
int space1 = 0;
int space2 = 0;
int space3 = 0;
int end = 0;
int startline = 0;
string holder;
for (int i = 0; i < cnt; i++)
{
holder = a[i];
lastnum = holder.find("lastname");
if (lastnum == -1)
{
lastnum = holder.find("surname");
lastnum = lastnum - 1;
}
space1 = holder.find(" ") + 1;
holder = holder.substr(space1+1, holder.length()-1);
space2 = holder.find(" ") + 2;
holder = holder.substr(space2+1, holder.length()-1);
space3 = holder.find(" ");
if (space3 > -1)
{
space3 = space3 + 3;
}
holder = a[i];
space2 = space1 + space2;
if (space3 > -1)
{
space3 = space2 + space3;
}
end = holder.size();
if (lastnum < space1)
{
last = holder.substr(space1, ((space2 - space1) - 1));
}
else
{
last = holder.substr(lastnum + 9, end);
}
if (space1 < lastnum)
{
first = holder.substr(startline, space1);
}
else
{
first = holder.substr(space2, (space3-space2));
}
if (space3 == -1)
{
middle = "";
}
else if (space3 != -1 && space1 > lastnum)
{
middle = holder.substr(space3, end);
}
else if (space3 != -1 && space1 < lastnum)
{
middle = holder.substr(space1, (space2 - space1));
}
}
}
string capt(string a[], string first, string middle, string last, int cnt)
{
char f;
char m;
char l;
for (int i = 0; i < cnt; i++)
{
l = toupper(last.at(0));
last = l + last.substr(1, last.length()) + ", ";
if (middle != "")
{
m = toupper(middle.at(0));
middle = m + middle.substr(1, middle.length());
}
f = toupper(first.at(0));
first = f + first.substr(1, first.length());
if (middle != "")
{
a[i] = last + first + middle;
}
else
{
a[i] = last + first;
}
}
}
string sort(string a[], int cnt)
{
string holder1;
string holder2;
string holder3;
for (int b = 0; b < cnt - 1 ; b++)
{
for (int c = 0; c < cnt - 1 - b; c++)
{
holder1 = a[c];
holder2 = a[c+1];
if (holder1.compare(holder2) > 0)
{
holder3 = holder2;
holder2 = holder1;
holder1 = holder3;
}
}
}
}
string print(string a[], int cnt)
{
for (int i = 0; i < cnt; i++)
{
if (a[i] != "")
{
outfile << a[i] << endl;
}
}
}
int main()
{
outfile.open("Namesout.out"); //open outfile
if (!outfile) // check outfile
{
outfile << "Output file did not open.\n";
return 1;
}
infile.open("nameslast.txt"); // open infile
if (!infile) // check infile
{
outfile << "Input file did not open.\n\n";
return 1;
}
int count;
string names[15];
string names2[15];
string fir;
string mid;
string last;
input(names, count);
cout << "Input End" << endl;
copy(names, names2, count);
cout << "Copy End" << endl;
breakup(names, fir, mid, last, count);
cout << "Breakup End" << endl;
capt(names, fir, mid, last, count);
cout << "Capt End" << endl;
sort(names, count);
cout << "Sort End" << endl;
cout << "Original List: " << endl;
print(names2, count);
cout << endl << "Arranged and Sorted List: " << endl;
print(names, count);
}
bennetk2 0 Newbie Poster
gerard4143 371 Nearly a Posting Maven
Taywin 312 Posting Virtuoso
bennetk2 0 Newbie Poster
bennetk2 0 Newbie Poster
bennetk2 0 Newbie Poster
Moschops 683 Practically a Master Poster Featured Poster
vidit_X 29 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.