Sorting program

I just can't get this program to work as required

Page 1 of 1

3 Replies - 1004 Views - Last Post: 04 November 2009 - 11:04 AM Rate Topic: -----

#1 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

Sorting program

Posted 04 November 2009 - 10:43 AM

This program is suppossed to read in words(strings) with no spaces,the maximum to be read is 20..it should print the entered words..maximum to be print is 15.Then its suppossed to sort the words in increasing alphabetical order and print them in the sorted order...But it doesn't work as I want it to...any help is greatly appreciated

#include <iostream>


using namespace std;
#include <string.h>


#include "ReadString.h"

void Sort (char **, int);

void main ()
	{
	const	int		MaxStrings	(20);
			
			char *	StringArray [MaxStrings];
			bool	Continue;
			int		NumStrings;

    
			cout << "Enter a maximum of 20 words:"<<endl;

	

	NumStrings = 0;
	do	{
		StringArray [NumStrings] = ReadString ();
		Continue = strlen (StringArray [NumStrings]) > 0;
		if (Continue)
				NumStrings++;
			else
				delete [] StringArray [NumStrings];
		} while (Continue && (NumStrings < MaxStrings));

	for (int i = 0; i < NumStrings; i++)
		cout << "StringArray [" << i << "] is " << StringArray [i] << endl;

	for (int i = 0; i < NumStrings; i++)
		delete [] StringArray [i];



	int		NumStringsRequested;
	int		NumStringsEntered;
	char **	Array2D;

	NumStringsRequested = 15;
	Array2D = new char * [NumStringsRequested];
	NumStringsEntered = 0;
	do	{
		Array2D [NumStringsEntered] = ReadString ();	// *(Array2D + NumStringsEntered) = ReadString ();
		Continue = strlen (Array2D [NumStringsEntered]) > 0;
		if (Continue)
				NumStringsEntered++;
			else
				delete [] Array2D [NumStringsEntered];
		} while (Continue && (NumStringsEntered < NumStringsRequested));

	for (int i = 0; i < NumStringsEntered; i++)
		cout << "Array2D [" << i << "] is " << Array2D [i] << endl;

	for (int i = 0; i < NumStringsEntered; i++)
		delete [] Array2D [i];
	delete [] Array2D;
	}

void Sort (char ** Array2D, int NumStrings)
	{
	char *	pTemp;
	bool	Sorted;

	NumStrings--;
	for (int i = 0; i < NumStrings; i++)
		if (strcmp (Array2D [i], Array2D [i + 1]) > 0)
				{
				pTemp			= Array2D [i];
				Array2D [i]		= Array2D [i + 1];
				Array2D [i + 1]	= pTemp;
				Sorted			= false;
				}
	}







Here is the readstring.cpp file

#include <iostream>

using namespace std;

#include <memory.h>		// for memcpy
#include "ReadString.h"

char * ReadString ()
	{
	const	int		FirstStringSize (20);
			int		CurrentLength	(0);
			int		CurrentSize		(FirstStringSize);
			char *	pString;
			char *	pTempString;
			char	c;

	pString = new char [CurrentSize + 1];
	while ((c = cin.get ()) != '\n')
		{
		pString [CurrentLength++] = c;
		if (CurrentLength  >= CurrentSize)
				{
				CurrentSize += FirstStringSize;
				pTempString = new char [CurrentSize + 1];
				memcpy (pTempString, pString, CurrentLength);
				delete [] pString;
				pString = pTempString;
				}
			else;
		}
	pString [CurrentLength] = '\0';
	return pString;
	}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 04 November 2009 - 11:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting program

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Sorting program

Posted 04 November 2009 - 10:53 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

Re: Sorting program

Posted 04 November 2009 - 10:59 AM

View PostJackOfAllTrades, on 4 Nov, 2009 - 09:53 AM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field




The problem I'm having is the code compiles fine..it reads the words fine and prints them out..But it just won't sort them, even though I have the sort function in the main program
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Sorting program

Posted 04 November 2009 - 11:04 AM

void main() is incorrect; it's ALWAYS int main() or int main(int argc, char **argv)

And, just putting a Sort function into your file isn't going to make it happen magically...you need to call the function!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1