Avatar of BrainDeadStudent
BrainDeadStudent
Flag for United States of America asked on

multiple &&'s

I am writing a program to compare and sort elements in an Array.  I am having much difficulty with printing my compare results. I can sort fine, but the compare is giving me trouble.  It doesn't seem to be completeing the &&'s.  Your help is appreciated!
I want this:
FMOR FORM
FMOR FROM
II am getting this:
FMOR CEFORS FORCES
FMOR EFGINOR FOREIGN
FMOR FMOR FORM
FMOR EFMORR FORMER
FMOR FMORS FORMS
FMOR ADFORRW FORWARD
FMOR DFNOU FOUND
FMOR FORU FOUR
FMOR EEFR FREE
FMOR DEEFMOR FREEDOM
FMOR CEFHNR FRENCH
FMOR DEFINR FRIEND
FMOR DEFINRS FRIENDS
FMOR FMOR FROM
FMOR FNORT FRONT
FMOR FLLU FULL
FMOR CFINNOTU FUNCTION
FMOR EFHRRTU FURTHER
FMOR EFRTUU FUTURE
using this code:
void findMatches(char* keys[],char* words[], int& count)
{       
	for (int keyIndex = 0; keyIndex < count; ++keyIndex)       
	{
		for (int keyIndex2 = 0; keyIndex2 < count; ++keyIndex2)       
		{
			if ((keys[keyIndex]&& keys[keyIndex2]==0) && (words[keyIndex]&& words[keyIndex2]!=0)) ;
			{                  
			out <<keys[keyIndex]<<" "<<keys[keyIndex2]<<" "<<words[keyIndex2]<<endl; 
				
			}                  
		}
	}
}

Open in new window

Programming Languages-Other

Avatar of undefined
Last Comment
BrainDeadStudent

8/22/2022 - Mon
Hugh McCurdy

First, I'm not  clear on what you are trying to accomplish.

Second, perhaps the problem can be solved by using more parenthesis.  For instance instead of
(keys[keyIndex]&& keys[keyIndex2]==0)
try
(( keys [ keyIndex ] && keys [ keyIndex2 ] ) == 0 )

Assuming what I've done is what you mean to do.
eric-atl

In line 7 from your code snippet above:  Why the semicolon after the if () ; { and not if () { with no ; ... is that a typo or the problem?  I have pasted my change below.  I do not think that () are the problem.
void findMatches(char* keys[],char* words[], int& count)
{       
	for (int keyIndex = 0; keyIndex < count; ++keyIndex)       
	{
		for (int keyIndex2 = 0; keyIndex2 < count; ++keyIndex2)       
		{
			if ((keys[keyIndex]&& keys[keyIndex2]==0) && (words[keyIndex]&& words[keyIndex2]!=0)) 
			{                  
			out <<keys[keyIndex]<<" "<<keys[keyIndex2]<<" "<<words[keyIndex2]<<endl; 
				
			}                  
		}
	}
}

Open in new window

BrainDeadStudent

ASKER
Hi:
Its 2:54 am after Valentines Day, thought after a lovely dinner with my spouse I would work on my program a little...he is now sleeping on the sofa...alone.  Heeeelp me!!!  I just can't figure this out.
My findMatches() function return everything in the Array.  I have played with this for several hours and I am cooo confused...
what I want:
FMOR FORM
FMOR FROM
// the word in alphabetical order by letter and the anagram in the Array that matches it.
What I am getting;
ACEF  FACE
ACFT  FACT
AFHIT  FAITH
AFLL  FALL
AFILMY  FAMILY
AFR  FAR
AFMR  FARM
AEFHRT  FATHER
AEFR  FEAR
ADEEFLR  FEDERAL
DEEF  FEED
EEFL  FEEL
EEFGILN  FEELING
EEFT  FEET
EFLT  FELT
EFW  FEW
DEFIL  FIELD
EFGIRU  FIGURE
EFGIRSU  FIGURES
DEFILL  FILLED
AFILN  FINAL
AFILLNY  FINALLY
DFIN  FIND
EFIN  FINE
EFIR  FIRE
FIMR  FIRM
FIRST  FIRST
ACFILS  FISCAL
EFIV  FIVE
FLOOR  FLOOR
DEFLLOOW  FOLLOWED
FGILLNOOW  FOLLOWING
DFOO  FOOD
FOOT  FOOT
FOR  FOR
CEFOR  FORCE
CEFORS  FORCES
EFGINOR  FOREIGN
FMOR  FORM
EFMORR  FORMER
FMORS  FORMS
ADFORRW  FORWARD
DFNOU  FOUND
FORU  FOUR
EEFR  FREE
DEEFMOR  FREEDOM
CEFHNR  FRENCH
DEFINR  FRIEND
DEFINRS  FRIENDS
FMOR  FROM
FNORT  FRONT
FLLU  FULL
CFINNOTU  FUNCTION
EFHRRTU  FURTHER
EFRTUU  FUTURE
Press any key to continue . . .
I don't understand why they all don't match!!!
Here is my entire code:
#include <iostream>
#include <fstream>
using namespace std;
char** read(const char* fileName, int& count);
char** wordArray(char* words[], int& count);
void createKeys(char* keys[], int& count);
void bubbleSort (char *keyArray , int count);
void findMatches(char* keyArray[],char* words[], int& count);
void displayResults(char* keys[],char* words[], int& count);
int main ()
{
	int wordCount;
	char** words = read("C:\\Users\\Mimi\\Desktop\\words.txt",wordCount);
	char** keys = wordArray(words, wordCount);
	createKeys(keys, wordCount);
	findMatches(keys, words, wordCount);
	//displayResults(keys, words, wordCount);
	return 0;
}
char** read(const char* fileName, int& count)
{
	ifstream countingStream(fileName);
	// first count them
	count = 0;
	while (true) 
	{
		char line[100];
		countingStream.getline(line, 100);
		if (strlen(line) == 0) 
		{
			break;
		}
		count += 1;
	}
	countingStream.close();
	ifstream readingStream(fileName);
	char** words = new char* [count];
	for (int index = 0; index < count; ++index) 
	{
		char line[100];
		readingStream.getline(line, 100);
		words[index] = _strdup(line);
	}
	readingStream.close();
	return words;
}
char** wordArray(char* words[], int& count)
{
	char** keyArray = new char*[count];
	for(int index = 0; index < count; ++index)
	{
		keyArray[index] = _strdup(words[index]);
	}
	return keyArray;
}
void createKeys(char* keys[], int& count)
{
	int length = 0;
	for(int keysIndex = 0; keysIndex < count; ++keysIndex)
	{
		length = strlen(keys[keysIndex]);
		bubbleSort(keys[keysIndex], length);
	}
}
void bubbleSort (char *keyArray , int count)
{
	char tempWord;
	for (int passes = 0; passes < count - 1; passes++)
		for (int compare = passes + 1; compare < count; compare++)
			if (keyArray[passes] > keyArray[compare])
			{
				tempWord = keyArray[passes];
				keyArray[passes] = keyArray[compare];
				keyArray[compare] = tempWord;
			}
}
void findMatches(char* keys[],char* words[], int& count) 
{   
for (int keyIndex = 1; keyIndex < count; ++keyIndex)       
{            
	if (strcmp(keys[keyIndex -1],  keys[keyIndex])!=0)            
	{                  
		cout << keys[keyIndex] << "  " << words[keyIndex]<< endl;                  
	}                  
} 
}
void displayResults(char* keys[],char* words[], int& count)
{      
	for (int keyIndex = 0; keyIndex < count; keyIndex++) 
		cout << keys[keyIndex] << " " << words[keyIndex] << endl;
}

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
BrainDeadStudent

ASKER
Sorry for the typos, i can't figure out how to edit it but what I mean't was they only print if I use the != otherwise nothing prints at all.  I think I am just trying to print the wrong "things" but I can't figure out how to capture the right thing to print
Hugh McCurdy

I'm still not sure I understand what you want.  My guess is you only want the results for FMOR and none of the words.  Is that right?
BrainDeadStudent

ASKER
Yes, exactly, I am sorry It was getting very late and I really was "Brain Dead".  I do not understand how I can do some things and then other things just totally escape me.  Was I absent that day or did my brain just have a hole in it on that day.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Hugh McCurdy

Perhaps what you should try are some debugging statements in your code.

I suggest also printing out

keys[keyIndex -1]

If it's still a mystery, I'd print out the lengths of both strings to make sure they match.  Perhaps you have a non graphic character in there.
BrainDeadStudent

ASKER
Hi,
Done that already.
 I need to iterate through the Keys and find a match,
I know it is "fmor",
then
 I need to iterate through the words and find the words from and form which both use those letters.
The pointers are what is messing me up.
I can not figure out how to do it properly.  I did rework much of my code so I will post it again.
#include <iostream>
#include <fstream>
using namespace std; 
char** read(const char* fileName, int& count); 
void selectionSort(int work[],const int size, bool(*compare)(int, int));
void swap(int * const element1Ptr, int * const element2Ptr);
bool ascending(int a, int b);
char** wordArray(char* words[], int& count); 
void createKeys(char* keys[], int& count); 
void bubbleSort (char *keyArray , int count); 
void findMatches(char* keys[],char* words[], int& count); 
int main () 
{ 
	int count; 
	char** words = read("C:\\Users\\Mimi\\Desktop\\words.txt",count);
	char** keys = wordArray(words, count); 
	createKeys(keys, count); 
	findMatches(keys,words,count); 
	return 0; 
} 
char** read(const char* fileName, int& count)
{
	std::ifstream countingStream(fileName);
	count = 0;
	while (true) {
		char line[100];
		countingStream.getline(line, 100);
		if (strlen(line) == 0) {
			break;
		}
		count += 1;
	}
	countingStream.close();
	std::ifstream readingStream(fileName);
	char** words = new char* [count];
	for (int index = 0; index < count; ++index) {
		char line[100];
		readingStream.getline(line, 100);
		words[index] = _strdup(line);
		std::cout << line << std::endl;
	}
	readingStream.close();
	return words;
}
void selectionSort(int work[],const int size, bool(*compare)(int, int))
{
	int smallOrBig;
	for( int i=0; i<size -1; i++)
	{
		smallOrBig =i;
		for( int index=i+1; index<size ; index++)
			if( !(*compare)(work[smallOrBig],work[index]))
				smallOrBig = index;
		swap(&work[smallOrBig],&work[i]);
	}
}
void swap(int * const element1Ptr, int * const element2Ptr)
{
	int hold = *element1Ptr;
	*element1Ptr = *element2Ptr;
	*element2Ptr = hold;
}
bool ascending(int a, int b)
{
	return a<b;
}
char** wordArray(char* words[], int& count) 
{ 
	char** keyArray = new char*[count]; 
	for(int index = 0; index < count; ++index) 
	{ 
		keyArray[index] = _strdup(words[index]);
	} 
	return keyArray; 
} 
void createKeys(char* keys[], int& count) 
{ 
	int length = 0; 
	for(int i = 0; i < count; ++i) 
	{ 
		length = strlen(keys[i]); 
		bubbleSort(keys[i], length); 
		cout<< keys[i]<< endl;
	} 
} 
void bubbleSort (char *keyArray , int count) 
{ 
	char tempWord; 
	for (int passes = 0; passes < count - 1; passes++) 
		for (int compare = passes + 1; compare < count; compare++) 
			if (keyArray[passes] > keyArray[compare]) 
			{ 
				tempWord = keyArray[passes]; 
				keyArray[passes] = keyArray[compare]; 
				keyArray[compare] = tempWord; 
			} 
} 
void findMatches(char* keyArray[],char* wordArray[], int& count)  
{  
	for (int i = 1; i < count; ++i)
	{
		int key1 = (int)keyArray[i];  
		for (int j = 1; j < count; ++j)
		{
			int key2 = (int)keyArray[j];
			if( key1.CompareTo(key2)==0)
			{
				cout << key1 << "  " << key2<< endl;
			}
		}
	}
}

Open in new window

Hugh McCurdy

The point was to share (at least some of) the debugging output with the rest of us.  Maybe one of us will see something unexpected and then know the answer.

Granted, this is how I often debug my code.  I add printing statements until I see something I didn't expect.
Your help has saved me hundreds of hours of internet surfing.
fblack61
BrainDeadStudent

ASKER
I have solved the matching/printing problem by creating an array to hold the values, now I have a new problem. If I sort the new array using a selection sort but don't print it, the program executes fully, If I attempt to print it the program hangs.  
The Debugger is telling me:
An unhandled exception of type 'System.AccessViolationException' occurred in Project_2.0.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The thread 'Win32 Thread' (0x1320) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1d10) has exited with code 0 (0x0).
The program '[7192] Project_2.0.exe: Managed' has exited with code 0 (0x0).
The program '[7192] Project_2.0.exe: Native' has exited with code 0 (0x0).
I "think" I am stepping off the array but I can't seem to figure out how I am doing it or if that is actually the problem.
before I sort the dupkeyArray I get this:
ACER    CARE
CEENRT    CENTER
ADEL    DEAL
AEHRT    EARTH
CEEPTX    EXCEPT
CEEPTX    EXPECT
EFLT    FELT
FMOR    FORM
FMOR    FROM
AEHRT    HEART
EHS    HES
HOW    HOW
ADEL    LEAD
EFLT    LEFT
AEMN    MEAN
AEMN    NAME
GHINT    NIGHT
NO    NO
NOW    NOW
NO    ON
I need to sort these by the first column, that is all I have left to do for this assignment.  I emailed my teacher and he said my sort function looks "good" keep trying!  But I need a little more help than that.

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

char** read(const char* fileName, int& count);
char** buildArray(char* words[], int& count);
void getKeys(char* keys[], int& count);
void bubbleSortKeys (char *arrayKey , int count);
void findDuplicate(char* keys[],char* words[],char*dupkeyArray[], int& count);
void selectionSort(char* dupkeyArray[],char* words[]);

int main ()
{
	int wordCount;
	char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",wordCount);
	char** keys = buildArray(words, wordCount);
	getKeys(keys, wordCount);
	char** dupkeyArray = new char *[wordCount];
	findDuplicate(keys, words, dupkeyArray,wordCount);
	selectionSort(dupkeyArray, words);
	return 0;
}

char** read(const char* fileName, int& count)
{
	ifstream countingStream(fileName);
	count = 0;
	while (true) 
	{
		char line[100];
		countingStream.getline(line, 100);
		if (strlen(line) == 0) 
		{
			break;
		}
		count += 1;
	}
	countingStream.close();

	ifstream readingStream(fileName);
	char** words = new char* [count];

	for (int index = 0; index < count; ++index) 
	{
		char line[100];
		readingStream.getline(line, 100);
		words[index] = _strdup(line);
	}
	readingStream.close();
	return words;
}

char** buildArray(char* words[], int& count)
{
	char** keyArray = new char*[count];
	for(int index = 0; index < count; ++index)
	{
		keyArray[index] = _strdup(words[index]);
	}
	return keyArray;
}

void getKeys(char* keys[], int& count)
{
	int length = 0;

	for(int keyIndex = 0; keyIndex < count; ++keyIndex)
	{
		length = strlen(keys[keyIndex]);
		bubbleSortKeys(keys[keyIndex], length);

	}
}
void bubbleSortKeys (char *arrayKey , int count)
{
	char wordHold;

	for (int pass = 0; pass < count - 1; pass++)

		for (int compare = pass + 1; compare < count; compare++)

			if (arrayKey[pass] > arrayKey[compare])
			{
				wordHold = arrayKey[pass];
				arrayKey[pass] = arrayKey[compare];
				arrayKey[compare] = wordHold;
			}
}

void findDuplicate(char* keys[],char* words[],char*dupkeyArray[], int& count)
{
	for (int i = 1; i < count; ++i) 
	{
		for (int j = 1; j < count; ++j) 
		{
			if ((strcmp(keys[i],  keys[j])==0)&& (strcmp(words[i],words[j])!=0))
			{
				dupkeyArray [i]= keys[i];
				cout.right;
				cout << dupkeyArray[i]<<"    "<< words[i]<<"  \n";
			} 
		}
	} 
}


void selectionSort(char* dupkeyArray[],char* words[])
{
	int i, j;
	char *temp;   // holding variable
	for (i=0; i< 32 ; i++)    // element to be compared
	{
		for(j = (i); j < 32; j++)   // rest of the elements
		{
			if (dupkeyArray[i] < dupkeyArray[j])          // descending order
			{
				temp= dupkeyArray[i];          // swap
				dupkeyArray[i] = dupkeyArray[j];
				dupkeyArray[j] = temp;
			}
		}

	}

	//for (i=0; i< 32 ; i++)
	// cout << dupkeyArray[1]/*<<"    "<< words[1]*/<<"  \n";
}

Open in new window

BrainDeadStudent

ASKER
I am sooo sorry, I guess I misunderstood something,  I don't know how I am ever going to do this when I have a life!!!  All I need to do now is sort the words so that words like form and from which should be equal appear next to other in the list.  the list should look like :
care race
except expect
form from .....
I still can't sort the second time but I am working on it!  help is GREATLY appreciated!
Hugh McCurdy

I'd still print progress.  I typically write in C, so I usually use printf for debugging.  Something like

printf ( "1\n" );

... code ...

printf ( "2\n" );

... code ...

and so forth.  Then I see how far the program gets before it stops.  It narrows down where the error is.  This has worked well except in one case (and that bug was in the DLL's).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
BrainDeadStudent

ASKER
I got an email from the teacher(SURPRIZE!) he gave me this tip.  Try sorting by the ascii value as care and race should be equal since they are anagrams.  I have no freaking clue how to do that.  I have been sorting alphabetically and numerically but how do I do it by ascii value?
Hugh McCurdy

Wonder if he means the sum of the ascii values.

Here's an ascii table.

http://www.asciitable.com/

You can also print the ascii value of characters in the string.  Let's say you have the string
char Buf[5] = "care";

printf ( "%c\n", Buf [ 0 ] );  will give you a c

but

printf ( "%d\n", Buf [ 0 ]; will give you  99

Hope this helps.
BrainDeadStudent

ASKER
Hello Again,  So I have gotten quite far but look how long it has taken me!  Anywho... I have a NEW problem now, Everything works until I get to the final
cout  statment.  The program seems to complete but then it ends unexpectantly.  Again I think I am stepping off the array but I can't seem to figure it out.  I have come so far with this, it seems like everytime I find a solution(with a little help) I create a new one.
My output is now:
  Loading...
  C:\Users\Mimi\Desktop\words[1].txt
  File is Loaded...
  There are 1000 Records...

  KEY     WORD    WORD
---------------------------------
ACER  CARE  RACE
CEENRT  CENTER  RECENT
ADEL  DEAL  LEAD
AEHRT  EARTH  HEART
CEEPTX  EXCEPT  EXPECT
CEEPTX  EXPECT  EXCEPT
EFLT  FELT  LEFT
FMOR  FORM  FROM
FMOR  FROM  FORM
AEHRT  HEART  EARTH
EHS  HES  SHE
HOW  HOW  WHO
ADEL  LEAD  DEAL
EFLT  LEFT  FELT
AEMN  MEAN  NAME
AEMN  NAME  MEAN
GHINT  NIGHT  THING
NO  NO  ON
NOW  NOW  OWN
NO  ON  NO
NOW  OWN  NOW
OPST  POST  STOP
ACER  RACE  CARE
CEENRT  RECENT  CENTER
ASW  SAW  WAS
EHS  SHE  HES
OPST  STOP  POST
EEHRT  THERE  THREE
GHINT  THING  NIGHT
EEHRT  THREE  THERE
ASW  WAS  SAW
HOW  WHO  HOW
Which is perfect(except that I need to neaten it up)
when it prints the last line a popup says that the project has stopped working and terminates with no error codes.  If I use the debugger I get:
STATUS_STACK_BUFFER_OVERRUN encountered
The thread 'Win32 Thread' (0x12fc) has exited with code -1073740791 (0xc0000409).
The thread 'Win32 Thread' (0x14e8) has exited with code -1073740791 (0xc0000409).
The thread 'Win32 Thread' (0x10d4) has exited with code -1073740791 (0xc0000409).
The program '[3128] project_20.exe: Managed' has exited with code -1073740791 (0xc0000409).
The program '[3128] project_20.exe: Native' has exited with code -1073740791 (0xc0000409).
"OK Lucy... esplain it to me"
I have attached the text file too just in case.
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

char** read(const char* fileName, int& count);
char** keyArray(char* words[], int& count);
void createKeys(char* keys[], int& count);
void bubbleSortKeys (char *sortedKeys , int count);
void sortMatches(char* keys[],char* words[], int& count);

int main ()
{
	int counter=0;

	char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",counter);
	char** keys = keyArray(words, counter);
	createKeys(keys, counter);
	sortMatches(keys, words,counter);
	return 0;
}
char** read(const char* fileName, int& count)
{
	ifstream countingStream(fileName);
	// first count them
	count = 0;
	while (true) 
	{
		char line[100];
		countingStream.getline(line, 100);
		if (strlen(line) == 0) 
		{
			break;
		}
		count += 1;
	}
	countingStream.close();

	ifstream readingStream(fileName);
	char** words = new char* [count];
	cout<<"  Loading...\n  "<<fileName<<"\n  File is Loaded...\n  There are "<<count<<" Records...\n\n";
	for (int index = 0; index < count; ++index) 
	{
		char line[100];
		readingStream.getline(line, 100);
		words[index] = _strdup(line);
	}
	readingStream.close();
	return words;
}

char** keyArray(char* words[], int& count)
{
	char** keyArray = new char*[count];
	for(int index = 0; index < count; ++index)
	{
		keyArray[index] = _strdup(words[index]);
	}
	return keyArray;
}

void createKeys(char* keys[], int& count)
{
	int length = 0;

	for(int keyIndex = 0; keyIndex < count; ++keyIndex)
	{
		length = strlen(keys[keyIndex]);
		bubbleSortKeys(keys[keyIndex], length);
	}
}
void bubbleSortKeys (char *sortedKeys , int count)
{
	char wordHold;

	for (int pass = 0; pass < count - 1; pass++)

		for (int compare = pass + 1; compare < count; compare++)

			if (sortedKeys[pass] > sortedKeys[compare])
			{
				wordHold = sortedKeys[pass];
				sortedKeys[pass] = sortedKeys[compare];
				sortedKeys[compare] = wordHold;
			}
}

void sortMatches(char* keys[],char* words[], int& count)
{

	cout<<"  KEY"<<"     WORD"<<"    WORD\n";
	cout<<"---------------------------------\n";

	for (int i = 1; i < count; ++i) 
	{
		for (int j = 1; j < count; ++j) 
		{
			if ((strcmp(keys[i],  keys[j])==0)&& (strcmp(words[i],words[j])!=0))
			{

				char*dup1[1];// I know this could be tighter but i'm not sure how to do it yet
				char*dup2[1];
				char*dup3[1];
				dup1[i-1]=keys[i];
				dup2[i-1]=words[i];
				dup3[i-1]=words[j];

				cout<<dup1[i-1]<<"  "<<dup2[i-1]<<"  "<<dup3[i-1]<<"\n";
				
			} 

		}
	} 
}

Open in new window

words-1-.txt
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
BrainDeadStudent

ASKER
Ps..I truly hope that someday I will be able to answer questions like this for somebody else.  I really do love this stuff and I am getting better but I have soooo far to go.
Hugh McCurdy

You are getting better.

I'd go back to adding printf (or puts) statements after the last print in order to narrow down exactly where it fails.

My guess is when you are calling a function.  The question is which one?

Something like

puts("1");
...code...
puts("2");
...

Be sure to put one before each function call (after the printing section) and after each function call.  This way, when the printing stops, you'll know where it stopped.

Note: Later you may want to use fputs() and write to a log file.  When you do that, you want to follow each call to fputs(); with fflush();  Writing to files (using the "f" functions is buffered.  Writing to character devices, isn't.

Encouragement: Learning this sort of thing you'll often go through phases where it doesn't work.  Then it does work and you don't really understand why.  Then, suddenly, it's all clear.
BrainDeadStudent

ASKER
Alrighty then, you people with very big brains,
I am seriously stuck on this, I can list the Anagrams in Alphabetical order just fine, but there are duplicates, I can do it without the pointers but I can't seem to do it with them.
Here is my out from my code:
CARE    RACE
CENTER  RECENT
DEAL    LEAD
EARTH   HEART
EXCEPT  EXPECT
EXPECT  EXCEPT
FELT    LEFT
. . .
HOW     WHO
. . .
WHO     HOW

 I emailed my Teacher again to show hime my output and ask for a hint or some direction and this was his reply:
Sent: Thursday, February 18, 2010 5:45 PM
To: mnusbaum
Subject: Re: Project 2
No, this is not OK.
You should list anagrams only once.

When I say a small set, I mean it: start with the few words I gave in the posting for instance, or even less.
I bow to thee programming Gods & Goddess's but I really really need a little help! I am stuck here.  This is the largest program I have ever written and I am shocked that it works as well as it does but it has taken a lot of trial and error on my part.  I have gotten to the part where I don't know if I can go any further because I am completely out of ideas.  I have tried every kind of sort I think of or find an example for.
Thank-you in advance.
#include <iostream>
#include <fstream>
#include <string.h>
#include <list>


using namespace std;

char** read(const char* fileName, int& count);
char** buildArray(char* words[], int& count);
void getKeys(char* keys[], int& count);
void bubbleSortKeys (char *arrayKey , int count);
void sortMatches(char* keys[],char* words[], int& count);


int main ()
{
      int wordCount;
	  cout<<"\n  This Program finds Anagrams in a List of Words  \n";
	  cout<<"____________________________________________________\n\n";
	
	  char** words = read("C:\\Users\\Mimi\\Desktop\\words[1].txt",wordCount);
	  char** keys = buildArray(words, wordCount);
      char** unique= new char*[wordCount] ;
	  getKeys(keys, wordCount);
	  sortMatches(keys, words, wordCount);
	
      return 0;
}
char** read(const char* fileName, int& count)
{
  ifstream countingStream(fileName);
  // first count them
  count = 0;
  while (true) 
  {
            char line[100];
            countingStream.getline(line, 100);
            if (strlen(line) == 0) 
            {
                  break;
            }
            count += 1;
}
  countingStream.close();
if(count==0)
{
  cout<<"\n  Loading...\n  "<<fileName<<"\n\n  File is Empty or did not Load...\n  There are "<<count<<" Records...\n  Check Path or File Name\n\n";
}
else
{
	cout<<"\n  "<<fileName<<"  is loaded...\n  There are  "<<count<<"  Records.\n\n";
}
  ifstream readingStream(fileName);
  char** words = new char* [count];
  



  for (int index = 0; index < count; ++index) 
  {
            char line[100];
            readingStream.getline(line, 100);
            words[index] = _strdup(line);
           
  }
  readingStream.close();
  return words;
}

char** buildArray(char* words[], int& count)
{
	char** keyArray = new char*[count];
	for(int index = 0; index < count; ++index)
	{
		keyArray[index] = _strdup(words[index]);
	}
	return keyArray;
}

void getKeys(char* keys[], int& count)
{
	int length = 0;

	for(int keyIndex = 0; keyIndex < count; ++keyIndex)
	{
		length = strlen(keys[keyIndex]);
		bubbleSortKeys(keys[keyIndex], length);
	}
}
void bubbleSortKeys (char *arrayKey , int count)
{
      char wordHold;

      for (int pass = 0; pass < count - 1; pass++)
      
             for (int compare = pass + 1; compare < count; compare++)

                   if (arrayKey[pass] > arrayKey[compare])
                   {
                        wordHold = arrayKey[pass];
			            arrayKey[pass] = arrayKey[compare];
			            arrayKey[compare] = wordHold;
   		           }
}

void sortMatches(char* keys[],char* words[], int& count)
{

	cout<<"  Anagrams from Text File\n";
	cout<<"---------------------------\n";

	char** unique= new char*[count] ;
char** unique2= new char*[count] ;
	for (int i = 1; i < count; ++i) 
	{
		for (int j = 1; j < count; ++j) 
		{
			if ((strcmp(keys[i],  keys[j])==0)&& (strcmp(words[i],words[j])!=0))
			{
				unique[i]=keys[i];
				cout<<"    ";
int counter=counter+1;	
			
			
			cout.width(4);
cout<<left<<counter;

				cout.width(8);
				
				cout<<left<< words[i];
				cout.width(8);
				cout<< words[j]<<endl;
				
			}
		}
	} 
	
   
}

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
BrainDeadStudent

ASKER
Sorry about the indents on the last function.  I must have done something wrong there too after deleting the stuff that doesn't work
BrainDeadStudent

ASKER
Ok Guys,
Sorry this took me so long but we were away.  While I was gone I figured it out... the second for statement in my findMatches() was wrong it was:
for (int j = 1; j < count; ++j)
I replaced it with:
for (int j = i+1; j < count; ++j)  which makes it work perfectly!!  Who would have thought it was just a +1 missing for all the time it took me.
So I shall split the solution with you hmccurdy, because your advice did help too!
Now on to the next problem and you know I have a question!!
ASKER CERTIFIED SOLUTION
Hugh McCurdy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BrainDeadStudent

ASKER
Helped a lot with some other issues but ultimatly I found the answer myself after banging my head on the desk a few times.  But the help I got was very useful to some other programs I am working on.  Thanks much
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23