Sorting Homework Help

So I'm trying to do my C++ Homework and I feel like i'm really close to finishing it but I can't figure out an error. This program should create an array of objects and pass them into a bubble sort. I'm supposed to then filter it into my function called alphaGreater which should sort it alphabetically. Its all going pretty good except I get an error when I pass my object to my sorting function it says:

object.cpp: In function 'int main()':
object.cpp:51:22: error: invalid initialization of non-const reference of type '
PhoneEntry&' from an rvalue of type 'PhoneEntry*'
object.cpp:23:6: error: in passing argument 1 of 'void mySort(PhoneEntry&, int)'


Not sure what to do now and it's due tomorrow! Here's my driver where i'm having the problem:

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
#include <iostream> 
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry& arr, int size)
{
    bool inOrder = false;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            inOrder = arr.alphaGreater(arr&);
        }
    }
};

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
        }
        mySort(entry&, count);
        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}


I linked my headers into pastebin for easy formatting:

Phone Entry Header http://pastebin.com/f6Xvu398
Phone Number Header http://pastebin.com/ytdi8x3D
What I'm Sorting http://pastebin.com/HE8Rsmbg

The sorting is just dummy numbers. Thanks in advance for the help!
Last edited on
try this:
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
#include <iostream> 
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry* arr, int size)
{
    bool inOrder = false;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            inOrder = arr[j].alphaGreater(arr);
        }
    }
}

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
        }
        mySort(entry, count);
        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}


I have no idea what you are realy talking about cos you headers are NOT in those pastebis, so this is probably wron. post the headers and the array here so we can see them!
Sorry I fixed the links. It was appending the parenthesis to the link :S
I'm thinking this line is nonsense:
mySort(entry&, count);
mySort expects a pointer to an Entry, but received the address of a pointer to an entry. Drop the '&' and it might work.


Unrelated to the errors, there's something odd in your code:

1
2
3
4
5
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            inOrder = arr[j].alphaGreater(arr);
        }

This piece of code is equal to this:
inOrder = arr[i-1].alphaGreater(arr);
Only the last item will have any impact on the final value of 'inOrder'.

You could try changing it to this:
1
2
3
4
5
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            inOrder = inOrder && arr[j].alphaGreater(arr);
        }

That way, if 'inOrder' is already false, it will remain false. In other words: inOrder will be true until it hits the first 'false'.

Ok I corrected some of the driver but it is telling me there's no operator[] in arr[j]. And really I don't see how this "Bubble Sort" passing values into alphaGreater is doing to sort anything alphabetically...

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
#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry &arr, int size)
{
    bool inOrder = false;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
			inOrder = inOrder && arr[j].alphaGreater(arr);
        }
    }
};

int main()
{	
	const int MAXNUM = 500;
	PhoneEntry entry[MAXNUM];
	ifstream filezilla;
	filezilla.open("phone.txt");
	int count = 0;
	
	if(filezilla)
	{
		while(count < MAXNUM && entry[count].readEntry(filezilla))
		{
			count++;
		}
		
		mySort(entry, count);
		
		for(int i = 0; i < count; i++)
		{
			entry[i].writeEntry(cout) << endl;
		}
	}
	else
	{
		cout << "404" << endl;
	}

	return 0;
}
It's not. It should have the word swap() in there somewhere. At least once.
Well this is the original sort that I needed to edit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void mySort(double array[], int size)
{
    // Easy, but inefficient
    // Modified to quit if it finds
    // array in order on any pass
	double temp;
    bool inOrder = false;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            if (array[j] > array[j + 1])
            {
                inOrder = false;
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}


so I guess I need to change in mine

1
2
3
4
5
6
7
if (alphaGreater(arr)[j])
            {
                inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }


Does that seem right?
Depends on what alphaGreater does. Does it check for (arr[j] > arr[j+1])? If so, it's probably correct.
(What exactly is your assignment? Adjusting a BubbleSort to work on Strings instead of Ints?)
I added headers to my main post inside pastebin. The assignment is supposed to (1) read a file of strings and add some formatting. Then I need to sort the string into alphabetical order. There's a bunch of restrictions and such to this. I personally don't see the need for "alphaGreater()" but it is a necessity. All the functions are necessities so far. I edited the sort function but I get a bunch of "no match for operator[] in arr[j]" I'm thinking I wouldn't even need the second loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void mySort(PhoneEntry &arr, int size)
{
    bool inOrder = false;
	string temp;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
			if(arr.alphaGreater(arr[j]))
			{
				inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
			}
        }
    }
};
Topic archived. No new replies allowed.