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

Edited 5 Years Ago by bennetk2: n/a

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.

Your description is not understandable. When you run a program, it doesn't read the program so it can't get stuck at the end after "It reads the last line of code in the function" Please describe what actually happens to make you think it's not working.

Your description is not understandable. When you run a program, it doesn't read the program so it can't get stuck at the end after "It reads the last line of code in the function" Please describe what actually happens to make you think it's not working.

I build/run it. I get some output, that I generated to see where in the code I was getting 'stuck' on.

"Out of Input Function

Out of Copy Function


0

Taylor, Marie Denise

1

Thomas, James Wilis

2

Brown, Stone Rock

3

Lea, High Lee

4

Reynolds, Reynolds

5

Mack, Russell

6

Lewis, Michelle Tee

7

Marshall, John Mark

8

Moto, Ahsey

9

Vey, O E

10

Knocksee, Twosee Or


end

Process returned -1073741819 (0xC0000005) execution time : 4.423 s
Press any key to continue.
"

I get the cout statement of "end". Then the complier/runner whatever flakes out and I get a windows message that says the program I am running has stopped working unexpectedly. I click okay then at the bottom of the output I get, this process returned thing. Why is this happening?

I get the cout statement of "end". Then the complier/runner whatever flakes out and I get a windows message that says the program I am running has stopped working unexpectedly. I click okay then at the bottom of the output I get, this process returned thing. Why is this happening?

"complier/runner whatever" -- try program... That's what you are writing.

Outputting end shows it finished the function and returned. So the problem may be after arrange() returns. That is unless arrange() wrote over memory it shouldn't have. Also, what happens if one of your holder.substr(); calls returns an error/not found?

When outputting trace messages like you are doing, follow these steps:
1) Add the name of the function to the message so you can trace the actual flow.
2) Don't output so many blank lines. It makes the trace difficult to read. None during a function. One blank when a function exits is OK.

My guess is the sort function is blowing up. Add some trace lines there. Since you never put holder1 nor holder2 back into a[??], you never really sort anything. You just go through the motions and accomplish nothing. You don't even need holder1 and holder2. You can use a[c] and a[c+1] and the sort should function properly. Or nearly so.

That is unless arrange() wrote over memory it shouldn't have.

How do I check for this? The program is never getting back into the main(), after arrange(). I am thinking that you may be correct. How do I check for this error, and fix it?

See the youtube vid link in my sig. if you're using Visual Studio.
Also, that arrange function is quite large and I recommend breaking it down into smaller pieces where prudent.

How do I check for this? The program is never getting back into the main(), after arrange(). I am thinking that you may be correct. How do I check for this error, and fix it?

Start outputting useful information, like index values and other variables that, if wrong, can cause problems. Like if i>14 for example.

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