Functions not sorting arrays alphabetically or numerically as intended.

I wrote two functions, both of them open a "scores.txt" file and stores the contents into a scores array and a names array.
◦ sort_by_score(int order) sorts the arrays, displaying them from lowest to highest score if "order" is negative, and highest to lowest if "order" is zero or positive.
◦ sort_by_name(int order) sorts the arrays, displaying them from A to Z if "order" is negative, and Z to A if "order" is zero or positive.

"scores.txt" reads as follows:

92 Wilfred
38 Frederick
77 Pamelia
50 Josette
33 Carri
70 Tynisha
77 Leonel
87 Charmain
29 Chrissy
49 Arletta
31 Richard
61 Tyrone
18 Dannie
63 Beryl
57 Nestor
72 Tarah
18 Winnifred
93 Salley
90 Frances
25 Dave

I have gotten this far, but the output seems to be just a cluster of numbers that are totally unrelated to the scores listed in the "scores.txt" file and no names are printed. I'd really like to know what I'm doing wrong.

Any help is appreciated! (first semester programmer, total newbie)

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>

using namespace std;

void sort_by_score (int order);
void sort_by_name (int order);

int main () //main must remain as this
{
    sort_by_score(1);
    cout << endl;
    sort_by_name(-1);
    
    return 0;
}

void sort_by_score(int order) //sorting score array
{
    const int SIZE=20;
    int score[20], tempScore;
    string name[SIZE];
    string tempName;
    ifstream inFile;
    inFile.open ("scores.txt");
    bool swap = false;
    
    
    for (int i = 0; i < 19; i++)
    {
        inFile >> score[i]>>name[i];
        
        if (score [i] < score [i+1])
        {
            tempScore = score [i];
            score[i] = score [i+1];
            score [i+1] = tempScore;
            
            tempName = name [i];
            name [i] = name [i+1];
            name [i+1] = tempName;
            swap = 1;
        }
        else
        {
            swap = 0;
        }
    }
    for (int i = 0; i < 19; i++)
    {
        cout <<score[i] << name[i];
        cout<<setw(20)<<left<<name[19]<<score[19]<<"\n";
    }
}

void sort_by_name(int order) //sorting name array
{
    const int SIZE=20;
    string name[SIZE];
    int tempScore;
    string tempName;
    ifstream inFile;
    inFile.open ("scores.txt");
    int score[20];
    bool swap = false;
    
    for(int i = 0; i < 19; i++)
    {
        inFile >> score[i]>>name[i];
        
        if (name [i] < name [i+1])
        {
            tempName = name [i];
            name [i] = name [i+1];
            name [i+1] = tempName;
            
            tempScore = score [i];
            score [i] = score [i+1];
            score [i+1] = tempScore;
            swap = 1;
        }
        else
        {
            swap = 0;
        }
    for (int i = 0; i < 19; i++)
    {
        cout << score[i] << name[i];
        cout<<setw(20)<<left<<name[19]<<score[19]<<"\n";
    }
    
}

}

Last edited on
On line 73, name[i+1] has an indeterminate value. You haven't read anything into it yet.

Same with score[i+1] on line 35.
Topic archived. No new replies allowed.