I am getting a big problem at the end of my arrange function. It reads the last line of code in the function, but then never leaves the function. I don't care about output at this point. I just want to figure out why I can't leave the function and at least run the entire program. I can fudge stuff around then to tweak the correct output.
Any help. I am a student. I have written this code, just need some help. This is my second programming class, so please 'dumb' it down for me. Thanks.
#include <iostream>
#include <iomanip> // set width and decimal
#include <fstream> // use file input and output
#include <string> // use strings
using namespace std;
string input(string, int);
string copy(string, string, int);
string arrange(string, int);
string sort(string, int);
string print(string, int);
ifstream infile;
ofstream outfile;
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 arrange(string a[], int &cnt)
{
int lastnum = 0;
int space1 = 0;
int space2 = 0;
int space3 = 0;
int end = 0;
int startline = 0;
string holder;
char l;
char f;
char m;
string first;
string middle;
string last;
for (int i = 0; i < cnt; i++)
{
if (a[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);
cout << endl << i << endl;
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));
}
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 (space3 != -1)
{
a[i] = last + first + middle;
}
else if (space3 == -1)
{
a[i] = last + first;
}
cout << endl << a[i] << endl;
}
}
cout << endl << endl << "end" << endl;
}
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()
{
string names[15];
string names2[15];
int count = 0;
outfile.open("Namesout.out"); //open outfile
if (!outfile) // check outfile
{
outfile << "Output file did not open.\n";
return 1;
}
infile.open("names.txt"); // open infile
if (!infile) // check infile
{
outfile << "Input file did not open.\n\n";
return 1;
}
input(names, count);
cout << "Out of Input Function\n\n";
copy(names, names2, count);
cout << "Out of Copy Function\n\n";
arrange(names, count);
cout << "Out of Arrange Function\n\n";
sort(names, count);
cout << "Out of Sort Function\n\n";
outfile << "First Set of Names:\n\n";
print(names2, count);
cout << "Out of Print 1 Function\n\n";
outfile << "Sorted and Arranged Names:\n\n";
print(names, count);
cout << "Out of Print 2 Function\n\n";
return 0;
}