#include <iostream>
#include <cstring>
usingnamespace std;
constint MaxNames = 21;
constint 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;
}
}
}
}
}
Your Sort function prototype mis not correct. It must be staticvoid 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
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...
#include <iostream>
#include <cstring>
usingnamespace std;
constint MaxNames = 21;
constint MaxChars = 16;
char Array[MaxNames][MaxChars];
char temp[MaxNames][MaxChars];
int i, j, pass;
staticvoid 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;
}
}
}
}
}
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/