Hi I'm New Here. Although I have been reading in the past and appreciate all the help that the board has given me already. I have a homework assignment that I need the user to enter how many names they want to enter, then enter that number of names, display them, then sort them and display them. However, my sort function is blowing up and I can't figure out for the life of me why. I have been looking at this thing for so long and am so confused as to why it is not working. Any help is appreciated.

#include <iostream>


using namespace std;

#include <string.h>

#include "ReadString.h"
#include "MySort.h"

void main ()
	{

	long	i;
	long	NumNames;
	char **	pNames;

	cout << "How many names do you want? ";
	cin >> NumNames;
	cin.get ();
	cout << "Enter " << NumNames << " strings: " << endl;
	pNames = new char * [NumNames];
	for (i = 0; i < NumNames; i++)
		pNames [i] = ReadString ();
	cout << "You entered:\n" << endl;
	for (i = 0; i < NumNames; i++)
		cout << pNames [i] << endl;
	for (i = 0; i < NumNames; i++)
		delete [] pNames [i];
	delete [] pNames;

	SortNames(pNames, NumNames);
	cout << "After sorting the names" << pNames [NumNames] << endl;
	}

And here is my Sort file (it's a bubble sort)

#include "MySort.h"
#include <locale>


void SortNames (char ** pNames, long NumNames)
	{
	char * temp ;
	char Letter1, Letter2 ;
	bool Sorted ;
	do {
		Sorted = true ;
		NumNames-- ;
		for (int i = 0; i < NumNames; i++ )
			{
			for ( int j = 0; j < 20; j++ )
				{
				Letter1 = toupper(pNames[i][j]) ;
				Letter2 = toupper(pNames[i+1][j]) ;
				if (Letter1 > Letter2)
					{
					temp = pNames[i] ;
					pNames[i] = pNames[i+1] ;
					pNames[i+1] = temp ;
					Sorted = false ;
					break ;
					}
				else
					if (Letter1 == Letter2)	
						Sorted = false ;
					else
						break ;  // in order for now
				}
			}
		} while (!Sorted) ;
	}

Recommended Answers

All 6 Replies

Can you give an example of your input and output? (what you expect and what you are currently getting)

Can you give an example of your input and output? (what you expect and what you are currently getting)

The input is dynamic. If the users states they want to enter 3 names, the array size is 3, if they say 4, it is 4. The input is working fine and displays the names. The sort is where it is bombing. If the user entered 3 names (for example), the sort should sort them alphabetically and then display the list of names as they were sorted. However, the it completely bombs at that point and I have to break the program and stop debugging.

It helps to know what your exact output is. If it's completely gibberish it could point to a pointer error, or if it's just not sorting correctly then the problem is in your nested loops. Though I am a little confused by these:

Letter1 = toupper(pNames[i][j]) ;
Letter2 = toupper(pNames[i+1][j])

is pNames a 2D array?

It helps to know what your exact output is. If it's completely gibberish it could point to a pointer error, or if it's just not sorting correctly then the problem is in your nested loops. Though I am a little confused by these:

Letter1 = toupper(pNames[i][j]) ;
Letter2 = toupper(pNames[i+1][j])

is pNames a 2D array?

Yes it is a 2D Array. Can I say I really hate Array's ugg. I am getting this error.

This pops up...Unhandled exception at 0x00be21e1 in Lab 11a.exe: 0xC0000005: Access violation reading location 0xfeeefeee.

When I break and drill into the error, I get CXX0030: Error: expression cannot be evaluated

I didn't look at your code too closely but here are couple suggestions.
1. You should use string instead of char; string has built in compare() which you can use which eliminates the use of a 2D array and you can fous on the sort algorithm instead of comparing each char in each word
http://www.cplusplus.com/reference/string/string/compare/
2. If you have to use char, your problem is probably caused by access out of bound in the array, meaning different name have different sizes, so your need to make this for loop check dynamic to the size of the name not 20

for ( int j = 0; j < 20; j++ )
				{
				Letter1 = toupper(pNames[i][j]) ;
				Letter2 = toupper(pNames[i+1][j]) ;
				if (Letter1 > Letter2)
					{
					temp = pNames[i] ;
					pNames[i] = pNames[i+1] ;
					pNames[i+1] = temp ;
					Sorted = false ;
					break ;
					}
				else
					if (Letter1 == Letter2)	
						Sorted = false ;
					else
						break ;  // in order for now
				}
			}

This pops up...Unhandled exception at 0x00be21e1 in Lab 11a.exe: 0xC0000005: Access violation reading location 0xfeeefeee.

Well, you delete [] everything you've allocated, and then try to use that memory -- obviously this will not work.
Rather do ..

... 
  SortNames(pNames, NumNames);
  cout << "After sorting the names" << pNames [NumNames] << endl;

  // Done with the memory, delete it ...
  for (i = 0; i < NumNames; i++)
    delete [] pNames [i];
  delete [] pNames;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.