Uggh, why won't this compile?!?

I wrote this code out and for whatever reason, I am getting a error at the following lines..

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
#include <iostream>
#include <cstring>
 
using namespace std;
 
const int MaxNames = 21;
const int MaxChars = 16;
char Array[MaxNames][MaxChars];
char temp[MaxNames][MaxChars];
int i, j, pass,
 
//static Sort(char Array[][MaxChars], char temp[][MaxChars], pass);
// Its throwing a C2059 syntax error over sort...but why? 
void main()
    {
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "This program will allow you to enter names and sort them in alphabetical order." << endl;
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "First, how many sets of names you will enter? (Must be less than 20): ";
    cin >> pass;
 
    if (pass==0)
        cout << "FATAL ERROR: You have entered 0 or a character. Please restart the program and try again." << endl;
        system("PAUSE");
 
    while (pass < 0 || pass > 20)
        { 
        cout << "ERROR: You cannot enter less than 1 or more than 20 numbers. Try again: ";
        cin >> pass;
        }
 
    for (i=0; i<pass; i++)
        {
        cout << "Enter a name: ";
        cin.getline(Array[i],MaxChars);
        }
 
    cout << endl << "Unsorted: ";
        for (int i=0; i<pass; i++) 
            {
            for (int j=0; j<MaxChars; j++)
                cout << Array[i][j];
            }
 
    Sort(Array[MaxNames][MaxChars], temp[MaxNames][MaxChars], pass);
 
    cout << endl << "Sorted: ";
        for (int i=0; i < pass; i++) 
            {
            for (int j=0; j<MaxChars; j++)
                cout << Array[i][j];
            }
    }

//It throws a 3861 error below over the sort function..by my prototype is correct 

void Sort(char Array[MaxNames][MaxChars], char temp[MaxNames][MaxChars], int pass)
    {
    bool IsSorted(false);
    while(!IsSorted)
        {
        IsSorted=true;
        for (i=0; i<pass-1; i++)
            {
            if (Array[i][0]>Array[i+1][0])
                {
                for (i=0, j=0; i<MaxNames-1, j<MaxChars-1; i++, j++)
                    {
                    temp[i][j]=Array[i][j];
                    Array[i][j]=Array[i+1][j];
                    Array[i+1][j]=temp[i][j];
                    IsSorted=false;
                    }
                } 
            }
        } 
    }
 
Take a reeeaaally close look at the end of the line before the static sort
int i, j, pass, should be be int i, j, pass;

Your Sort function prototype mis not correct. It must be
static void Sort(char Array[MaxNames][MaxChars], char temp[MaxNames][MaxChars], int pass);

EDIT: You are calling Sort() function incorrectly on line 45:
Sort(Array, temp, pass); // this is the correct way

Sort(Array[MaxNames][MaxChars], temp[MaxNames][MaxChars], pass); is not valid because you are passing a single character to the function instead of an array.

EDIT_2: You can use std::strings instead of char arrays
Last edited on
You guys rock! I have implemented in the edits and it compiles now, but it is skipping the first "enter name here" line entry, and then it will not show me the sorted names...

Any ideas?
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
#include <iostream>
#include <cstring>
 
using namespace std;
 
const int MaxNames = 21;
const int MaxChars = 16;
char Array[MaxNames][MaxChars];
char temp[MaxNames][MaxChars];
int i, j, pass;
 
static void Sort(char Array[][MaxChars], char temp[][MaxChars], int pass);

void main()
    {
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "This program will allow you to enter names and sort them in alphabetical order." << endl;
    cout << "-------------------------------------------------------------------------------" << endl;
    cout << "First, how many sets of names you will enter? (Must be less than 20): ";
    cin >> pass;
 
    if (pass==0)
        cout << "FATAL ERROR: You have entered 0 or a character. Please restart the program and try again." << endl;
        system("PAUSE");
 
    while (pass < 0 || pass > 20)
        { 
        cout << "ERROR: You cannot enter less than 1 or more than 20 numbers. Try again: ";
        cin >> pass;
        }
 
    for (i=0; i<pass; i++)
        {
        cout << "Enter a name: ";
        cin.getline(Array[i],MaxChars);
        }
 
    cout << endl << "Unsorted: ";
        for (int i=0; i<pass; i++) 
            {
            for (int j=0; j<MaxChars; j++)
                cout << Array[i][j];
            }
 
    Sort(Array,temp,pass);
 
    cout << endl << "Sorted: ";
        for (int i=0; i < pass; i++) 
            {
            for (int j=0; j<MaxChars; j++)
                cout << Array[i][j];
            }
    }


void Sort(char Array[MaxNames][MaxChars], char temp[MaxNames][MaxChars], int pass)
    {
    bool IsSorted(false);
    while(!IsSorted)
        {
        IsSorted=true;
        for (i=0; i<pass-1; i++)
            {
            if (Array[i][0]>Array[i+1][0])
                {
                for (i=0, j=0; i<MaxNames-1, j<MaxChars-1; i++, j++)
                    {
                    temp[i][j]=Array[i][j];
                    Array[i][j]=Array[i+1][j];
                    Array[i+1][j]=temp[i][j];
                    IsSorted=false;
                    }
                } 
            }
        } 
    }


Last edited on
Did you fix this Jetter2? If not you may notice there are some curly braces missing around line 23-24.
However your sort function looks like a right bag of spanners!
Using std::strings would be better, like Null said. also take a look at the following link for help on this:
http://www.cplusplus.com/reference/stl/list/sort/
Last edited on
Topic archived. No new replies allowed.