Help with sorting a dynamic array by structure variables

Hi, I'm working through the book "Jumping into C++" and I'm having issues with this practice problem in the chapter about dynamically creating arrays. I've seen posts ABOUT this problem, but not with the issue I'm having. My array updates and holds structures fine, the problem is when I try to sort the list, it blanks out all entries to have no name and a .days of 0, can someone tell me what I'm doing wrong? Using console logs it seems like the temp variable in my swap function IS getting the correct variables, something is messing up in the actual swapping, or overwriting, I'm not sure.

The question is: Write a program that lets users keep track of the last time they talked to each of their friends.
Users should be able to add new friends (as many as they want!) and store the number of days ago that they last talked to each friend. Let users update this value (but don't let them put in bogus numbers like negative values). Make it possible to display the list sorted by the names of the friends of by how recently it was since they talked to each friend.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>

using namespace std;

struct Friend{
    string name;
    int days;
};

Friend *growArray(Friend *p_friend, int* size){ //Double the size of the array if it needs it
    Friend *new_array = new Friend[*size*2];
    for(int i =0; i<*size;i++){
        new_array[i]=p_friend[i];
    }
    *size *= 2;
    delete [] p_friend;
    return  new_array;
}

void addFriend(Friend *p_friend, int friends){ // Add a new friend and add parameters
    cin.get();
    cout << "\nFriend's Name: ";
    getline(cin, p_friend[friends].name,'\n');
    cout << "How many days since you've talked to "<< p_friend[friends].name << "? ";
    cin >> p_friend[friends].days;
}

int findNextSmallest(Friend *p_friend, int size, int currentIndex){ // find the smallest index remaining
    int smallestIndex = currentIndex;
    //Test each index against the smallest one, if it is, replace
    for(int i = currentIndex; i<size; i++){
        if(p_friend[i].days<p_friend[smallestIndex].days){
            smallestIndex = i;
        }
    }
    return smallestIndex;
}

void swapIndex(Friend *p_friend, int index1, int index2){ //swap the current index with the smallest
    Friend temp;
    temp.name = p_friend[index1].name;
    temp.days = p_friend[index1].days;
    p_friend[index1].name = p_friend[index2].name;
    p_friend[index1].days = p_friend[index2].days;
    p_friend[index2].name = temp.name;
    p_friend[index2].days = temp.days;
}

void sortFriends(Friend *p_friend, int size){
//go through array
for(int i = 0;i<size;i++){
    //For each index, find the smallest index past that, and swap it
    int smallestIndex = findNextSmallest(p_friend, size, i);
    swapIndex(p_friend, i, smallestIndex);
}

}

int main(){
        //set initial dynamic array size
        int size = 2;
        //set initial dynamic array index
        int friends=0;
        //create dynamic array
        Friend *p_friend = new Friend[size];
        //for while loop
        int running = 1;
        //Menu selection
        int sel = 0;
        //main loop
        while(running){
                for(int i = 0; i <friends; i++){
                        //print all listed friends
                    cout << i+1 << ". " << p_friend[i].name << endl << p_friend[i].days << " days.\n\n";
                }
                //print menu and take selection
                cout << "1. Add Friend\n2.Sort Friends\n3.Quit\n";
                cin >> sel;
                //process selection
                if(sel==1){
                        //dynamically enlarge array if it needs space
                    if(friends==size-1){
                        p_friend = growArray(p_friend,&size);
                    }
                    //add a friend to the list and update index
                    addFriend(p_friend, friends);
                    friends++;
                }
                if(sel==2){
                    sortFriends(p_friend,size); // sort friends
                }
                if(sel==3){
                    running = 0; // exit program

                }
        }
}


Sorry if my code is a mess or really inefficient I've only been learning for 2-3 days
Last edited on
Your selection sort is very well-written. (Good job!)

The problem is how you are managing your array. There are two pieces of information you should be tracking:

-- size of the array ("size")
-- number of elements used in the array ("friends")

When you sort your array, everything appears to be zero because the number of days in unassigned elements is zero, making them sort before the people you add. You should not be sorting "size" elements.

Hope this helps.
Ah! Thank you, I can't believe I didn't notice that
Topic archived. No new replies allowed.