Errors in int main

My code seems to be working, I know that the user input is correctly adding to the arrays, but when I compile i'm getting errors in main, "expected primary expression before..." Any advice would be great because I'm very new to C++.
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
  //The purpose of this program is to allow the user to input five names and five associated scores.
//The program should then print the list of top scorers on the screen in decending order.

#include <iomanip>
#include <iostream>
using namespace std;

const int size = 25;
char names[5][size];
int scores[size]; 

    

//This function should receive user input and pass the information along to the next function.

void initializeArrays(string names[],int scores[], int size) {
    
    
    cout << "Enter the name for score #1: ";
        cin >> names[0];
    cout << "Enter the score for score #1: ";
        cin >> scores[0];
    cout << "Enter the name for score #2: ";
        cin >> names[1];
    cout << "Enter the score for score #2: ";
        cin >> scores[1];
    cout << "Enter the name for score #3: ";
        cin >> names[2];
    cout << "Enter the score for score #3: ";
        cin >> scores[2];
    cout << "Enter the name for score #4: ";
        cin >> names[3];
    cout << "Enter the score for score #4: ";
        cin >> scores[3];
    cout << "Enter the name for score #5: ";
        cin >> names[4];
    cout << "Enter the score for score #5: ";
        cin >> scores[4];  

}



//This function should take the under input for the two arrays from the previous
//function and sort the data in decending order
void sortData(string names[], int scores[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (scores[count] > scores[count + 1])
{
temp = scores[count];
scores[count] = scores[count + 1];
scores[count + 1] = temp;
swap = true;
}
}
} while (swap);

}




//This functon should receive the sorted arrays from the previous function
//and display the top scorers in decending order
int displayData(const string names[], const int scores[], int size) {
    
    cout << "Top scorers: " << endl;
    
    
 return 0;   
}


int main () {
    
    initializeArrays (string names[], int scores[], int size);
    sortData(string names[], int scores[], int size);
    displayData(const string names[], const int scores[],int size);


return 0;
}



    


(1) In int main() you should be calling your functions, not declaring them. You don't need to specify types when invoking the function.

(2) names[] and scores[] should not be global variables; declare them in main() or the rest of your functions will get very confused.

(3) names[] is a string array, not a char array as you defined on line 9.

(4) For safety, use #include <string>



Remove all your global variables (lines 8-10) and rewrite main() as
1
2
3
4
5
6
7
8
9
int main () {
    const int size = 25;
    string names[size];
    int scores[size]; 
    initializeArrays (names, scores, size);
    sortData(names, scores, size);
    displayData(names, scores,size);
return 0;
}


Function initializeArrays() is VERY repetitive. Use a loop.
Last edited on
That is the missing piece that I have been struggling with. Thank you, it works perfectly now.
Topic archived. No new replies allowed.