Sorting two arrays

I have no idea what I am doing wrong. This is supposed to read names and years from a txt file and them put them into two arrays which should then be sorted. Where am I going wrong?

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

void dualSort (string names[], int years[], int size);
int main()
{   
    int years[60]={};
    string names[60]={};
    ifstream inputFile;
    string file;
    int number;
    string name;
    
    cout << "Please enter the filename: ";
    cin >> file;
    inputFile.open(file.c_str());
    
    for (int count=0; count<61; count++)
    {   
        if (count%2==0)
        {   
            cin >> name;
            names[count]=name;
        }

        
        else if (count%2 !=0)
        {   
            cin >> number;
            years[count]=number;
        }
         
         int size = count;
         dualSort(&names[count],&years[count], size);
   
   }
 
 
}


void dualSort(string names[], int years[], int size)
{
    int startScan, maxIndex;
    string tempName;
    int maxValue;
    for (startScan=0; startScan<(size-1); startScan++)
    {
        maxIndex = startScan;
        maxValue = years[startScan];
        tempName = names[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if (years[index] > maxValue)
            {
                maxValue = years[index];
                tempName = names[index];
                maxIndex = index;
            }
        }
        years[maxIndex] = years[startScan];
        names[maxIndex] = names[startScan];
        years[startScan] = maxValue;
        names[startScan] = tempName;

    }
         for (int count=0; count<61; count++)
           {
               cout << names[count];
               cout << years[count];
            }

}
Last edited on
I have no idea what I am doing wrong.


But, your compiler does. And it told you when you attempted to compile it. Generally, you'll want to share those sorts of things with anyone who might help you.

Line 29 is illegal. Line 34 is a syntax error. Arrays don't have members begin or end. Perhaps you meant to use a vector?
What? My issue is it doesn't do anything after I put the file name in. It compiles fine.
Considering we don't have the txt file nor what's in it It is kind of hard for us to help. What exactly does it do when you run this program? Does it continue to get user input?
I think so. It asks for the file name, you enter it, then nothing.

This is the contents of the file

Eric,1936
Andy,1947
Perry,2010
Fred,1937
James,2010
Cody,1988
Daryl,1955
Isaac,1957
Guy,1981
Allan,1984
Bob,1969
Sergio,1952
Albert,1963
Ed,1938
Ian,1972
John,1959
Dwight,1975
Kurt,1986
Clay,1982
Morris,1958
Marion,1990
Clifton,1953
Kirk,1951
Last edited on
Topic archived. No new replies allowed.