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

Wow what compiler are you using? I can't believe this wasn't flagged.

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

Both functions expect a return type of string but both functions return nothing.

Well, none of the function returns anything...

I was under the assumptions strings are pass by referenced automatically. and the only other thing i want returned is one count which i included & in it to pass by reference.

How would I return things then? Suggestion or example for one.

Wow what compiler are you using? I can't believe this wasn't flagged.

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

Both functions expect a return type of string but both functions return nothing.

If I return 0; I get


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3) execution time : 3.214 s
Press any key to continue.

I was under the assumptions strings are pass by referenced automatically.

They are not.

How would I return things then? Suggestion or example for one.

In this case, you don't need to. You are passing in a pointer to the string you want changed, so the changes are visible in the calling scope. All you need to do is change the declaration of the functions so that instead of returning string, they return void (i.e. nothing).

string input(string, int);

becomes

void input(string, int);

and likewise in the definitions.

Edited 5 Years Ago by Moschops: n/a

This article has been dead for over six months. Start a new discussion instead.