starting a "name sort" program

Writing a program for homework that involves sorting names in a 2D array using bubble sort.. I need help/direction getting the entry of names "entered" started. I don't understand my errors when compiled. I am trying this:

int main()
{
int i = NumberofNames;
cout << "How many names do you wish to enter? " << endl;
cin >> NumberofNames;

const int NumRows = 1;
int NumCols = NumberofNames;
char NamesEntered;
int length = 15;
char ** NameArray [] [];
NameArray = new int [NumberofNames, 15];
cout << "Please enter " << NumberofNames << " names: \n" << endl;
cin << NamesEntered;
cout << "You entered: " << NamesEntered << endl;

}
Last edited on
Start with these:

1. NumberofNames is not declared anywhere.

2. char** NameArray[][]; is an illegal declaration, but correcting it isn't what you want,
since that declaration is attempting to declare a 4D array.

3. new int[ NumberOfNames, 15 ]; again NumberOfNames is not declared, but even if
it was, this isn't what you want, since it will create a dynamically allocated array of 15
elements regardless of the value of NumberOfNames.
Fixed what was suggested, but still not sure how to get this to copy my names I enter, and get it to repeat those names entered. It seems it's set to whatever number you enter...


int main()
{
int NumberofNames;
cout << "How many names do you wish to enter? " << endl;
cin >> NumberofNames;

char length = 15;
char ** NameArray;
const int NumRows = 1;
char NumCols = NumberofNames;

cout << "Please enter " << NumberofNames << " names: \n";
for (int i = 0; i < NumCols; i++)
cin >> NumCols;
cout << "You have entered: \n" << NumCols << endl;
}
Last edited on
Please use code tags.

You will need to use a for loop to ask for a name repeatedly.
Is the for loop I used not doing that? Now it just locks on the enter names part.
It is taking in the amount of responses, and only displaying one letter a multiple of times. How can I change it up to read in each name. So if I choose 3, read in "Jim, Bob, Joe", and then reitterate my answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
	{
	int NumberofNames;
	cout << "How many names do you wish to enter? " << endl;
	cin >> NumberofNames;

	char ** NameArray;
	const int NumRows = 1;
	char NumCols = NumberofNames;
	char length = 15;
	NameArray = new char * [NumRows <= length];
    cout << "Please enter " << NumberofNames << " names: \n";
	for (int i = 0; i < length; i++)

	cin >> NumCols;

	cout << "You have entered: \n" << endl;
	for (int j = 0; j < length; j++)
	  cout << " " << NumCols;
Last edited on
cin >> NumCols;

reads a single character from the keyboard, since NumCols is defined to be a char, not a string.

We are not allowed to use strings for this project (that was already turned in, I had to send in my imcompleted code unfortunately)

At this point, I just wanna learn how to do it properly for upcoming test in May.
Topic archived. No new replies allowed.