using char, not strings for sorting

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. (cannot use strings... this is for a past due project, i want to understand what is needed so I can complete the final exam correctly in May)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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
if i understand you, you need to input some data(names), and after that you chose one number and need the name that is entered at that point( let us say the third name entered).

The program should look like this?

enter a name: Jim
enter a name: Bob
enter a name:Joe

enter a nmber: 2

the name is :Bob

???
I think what he meant is that you input the number of names, and then the names.

So. Here's what you can do: after the user inputs the number (n), create a char* array of size n, and put a function in a loop that repeats n times. On each iteration, the loop would get the name and store it in position n as well.

Then, for show, you can use another loop to list the names it got. It would be similar to the first one.

-Albatross
The program ultimately sorts the names in alphabetical order. I am only working with displaying what names were entered first...
Started over, redoing this... Same problem again... so what is needed in my "NumCols = NumCols" as a check to repeat my names entered???

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
#include <iostream>

using namespace std;

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

	char **		TwoDArray;
	char		NumCols;
	
	cout << "Enter " << NumofNames << " names to sort: " << endl;
	cin >> NumCols;
	TwoDArray = new char * [NumCols];	// create the coloumns pointers first
	for (int i = 0; i < NumCols; i++)
		TwoDArray [i] = new char [NumCols];	//creates an array for each row of my 2d array
		NumCols = NumCols;
	cout << "You entered: " << NumCols << endl;

	for (int i = 0; i < NumCols; i++)
		delete [] TwoDArray [i];	// erases the array's created for each row of the original array
	delete [] TwoDArray;			// erases the original array

	}
Am I placing my cin >> NumCols statement in the wrong place...?
I'd have used char * PsudoStringArray[] rather than char ** TwoPointerArray, but...

You should use a for loop around your second pair of cout and cin statements. Decrement NumofNames per iteration, and make sure it's greater than zero. You can even use NumofNames' value to determine where in the array the name is stored.

I'll leave the rest to you. :)

-Albie
for (int i = 0; NumofNames > 0; NumofNames--)

???

What I'm trying does not fix that when I type in: Alex
I only get the letter 'A'
i may be wrong bzt you are using char numcols, and a char type variable can store just one char sign, wich means that if u input Alex, the char stores just A, try using sometging like char name[50], and after geting the data u need (the name) you should copy it to a char[] or char* that has exactly that amount of space that the entered name needs. and you should define that char name[50] in the input loop so it is automaticly deleted after the end of the loop.

hope it helps ;)
All well, I was hoping you'd notice that you were putting a character array into a character. ;)

You can input directly into char * PsudoStringArray[length], though. You don't need another char.

Seeing as you might have some problem solving for the alphabetical order of these, try converting each individual character to an integer. Then, create another array that lists the order, so int arr[1] would store the number that you can plug into PSA[] to get the word, and would point to the one that would be at the top of the alphabetical stack, arr[2] would be the location of the one next down. To sort...

http://en.wikipedia.org/wiki/Bubble_sort

You'll need to run this several times and will need a special for loop for this. However...

Experienced C++ programmers: YES, I know there are vastly more efficient algorithms that can be run in log-space time, but a Bubble sort is easy to implement.

-Albatross
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
#include <iostream>

using namespace std;

char **Create2DArray (unsigned int NumRows, unsigned int NumCols)
{
	char **	TwoDArray;
	TwoDArray = new char * [NumRows];
	for (unsigned int i = 0; i < NumRows; i++)
		TwoDArray [i] = new char [NumRows];	//creates an array for 
       return TwoDArray;
}

void Free2DArray (char **TwoDArray, unsigned int NumCols)
{
	for (int i = 0; i < NumCols; i++)
		delete [] TwoDArray [i];	
	delete [] TwoDArray;		
}

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

    unsigned int NumCols = 20;  // or something that represents the max column size
	
    char **TwoDArray = Create2DArray (NumofNames, NumCols);
    for (unsigned int i = 0; i < NumCols; i++)
    {
        cout << "\nEnter a name ";
        cin >> NumCols;
    }
	cout << "You have entered: " << TwoDArray << endl;
    Free2DArray(TwoDArray, NumCols);
}
Still having issue... been setting this up differently, and it spits out some crud then seeks out some random number in memory...? I think my for statement is wrong in the (int main). What should my input be for cin? I want to say the Create2DArray, since NumCols is just my length, and NumOfNames is the number of names, not the actual char input...? Can someone compile and see this error?
cout << "\nEnter a name ";
cin >> NumCols;

numCols is defined as an integer

and the other thing is if u want to print a pointer's addres then u write cout << pointer; , but if u want to printe the data at that addres,: cout << *pointer;
I'm on it.

Whoops. For your for loop in main(), did you mean i < NumofNames;?

(After change)

Note what justAbeginner said. numCols is an integer, not a char []. In fact, you need numCols for something else it seems...
Try putting the names in a 2d character array... which it seems you have. Though I'd use char [][]...

(After change)

Now it works.

-Albatross
Last edited on
sooooo.. like this... but I'm not seeing where you want the char [] [] added?

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
#include <iostream>

using namespace std;

char **Create2DArray (unsigned int NumRows, unsigned int NumCols)
{
	char **	TwoDArray;
	TwoDArray = new char * [NumRows];
	for (unsigned int i = 0; i < NumRows; i++)
		TwoDArray [i] = new char [NumRows];	//creates an array for 
       return TwoDArray;
}

void Free2DArray (char **TwoDArray, unsigned int NumCols)
{
	for (int i = 0; i < NumCols; i++)
		delete [] TwoDArray [i];	
	delete [] TwoDArray;		
}

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

	int arraySize = 20;
    unsigned int NumCols = arraySize;  // or something that represents the max column size
    char ** TwoDArray = Create2DArray (NumofNames, NumCols);
    for (unsigned int i = 0; i < arraySize; i++)
    {
        cout << "\nEnter a name ";
        cin >> NumCols;
    }
	cout << "You have entered: " << TwoDArray << endl;
    Free2DArray(TwoDArray, NumCols);
}
I'd have used a char TwoDArray[NumofNames][20] in place of your char ** TwoDArray. However...

Do not input shtuff into NumCols. The types are incompatible. Also, I think using NumofNames in place of arraySize in
i < arraySize
Is in order. Else, your loop will run 20 times regardless of how many names need to be entered.

With my method you could input what you get from cin directly into the TwoDArray[i]. With your method it's also possible, but I'm not 100% sure how I'd go about it.

-Albatross
Getting a HEAP CORRUPTION ERROR
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
 #include <iostream>

using namespace std;

char **Create2DArray (unsigned int NumRows, unsigned int NumCols)
{
	char **	TwoDArray;
	TwoDArray = new char * [NumRows];
	for (unsigned int i = 0; i < NumRows; i++)
		TwoDArray [i] = new char [NumRows];	//creates an array for 
       return TwoDArray;
}

void Free2DArray (char **TwoDArray, unsigned int NumCols)
{
	for (int i = 0; i < NumCols; i++)
		delete [] TwoDArray [i];	
	delete [] TwoDArray;		
}

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

    unsigned int NumCols = 20;
    char ** TwoDArray = Create2DArray (NumofNames, NumCols);
    for (unsigned int i = 0; i < NumofNames; i++)
    {
        cout << "\nEnter a name ";
        cin >>  TwoDArray [i];
		
    }
	cout << "You have entered: " << *TwoDArray << endl;
    Free2DArray(TwoDArray, NumCols);
} 
That's why I recommended using char [][]. :D

-Albatross
Topic archived. No new replies allowed.