I have a program that deals with anagrams and for the most part the program works but on a few of the words that should be it says that they aren't and one says it is and it's not. can someone help?

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

string anagram(const string& firstWord, const string& secondWord);

int main()
{
	string firstWord;			// First word in the file 
	string secondWord;			// Second word in the file
	
	
	//opens data file
    ifstream in("filename.txt");

    // tell the computer to print out an error message if not in file
	if(!in.is_open())
	{
		cout << "Can't open input file\n";
	}

	// tell the program that while in the file continue to run 
    while( in >> firstWord >> secondWord)
	{
		cout << " " << firstWord << " " << secondWord << endl;
		anagram(firstWord, secondWord);
		cout << endl << " Press Enter to continue " << endl;
		cin.get();
	}

		in.clear();
		in.close();
		// print a closing message and exit program 
		cout << endl << "-=- Execution Terminated -=-" << endl << endl;
		cout << "==================================================" << endl 
         << endl;

    return 0;
}

//This function tests two inputted words to see if they are anagrams.

string anagram(const string& firstWord, const string& secondWord)
{
	//This gets the length of the first string.
	int lengthFirstWord = firstWord.length();
	
	//This gets the length of the second string.
	int lengthSecondWord = secondWord.length();

	//This sets the test to true to start the testing loops.
	bool testAnagram = true;

	//This is copying the second word for testing.
	string temp(secondWord);

	//These output the length of the strings.
	cout << " The length of the first word is: " << lengthFirstWord << endl;
	cout << " The length of the second word is: " << lengthSecondWord << endl;

	//The if statement test to see if the length of the words are equal.
	//If they are not then they are not anagrams.
	if (lengthFirstWord == lengthSecondWord)
	{
		int index1 = 0;

		//This starts the testing by getting the first character.
		while( index1 < lengthFirstWord && testAnagram != false)
		{
			//Takes one character and puts it in 1 character string.
			string testingChar = firstWord.substr(index1,1);

			//This sets the the tester to false. This will end the first loop if the
			//character is not found.
			testAnagram = false;

	//This tests a character from the first string against the characters of
	//the second string.
	for (int index2 = 0;index2 < lengthSecondWord; index2++)
	{
		//Takes one character and puts it in 1 character string.
		string testedChar = temp.substr (index2,1);

		//Test if the characters match.
		if (testingChar == testedChar)
		{
			//Changes the test condition to true to continue test the rest of the word
			//string.
			testAnagram = true;

			//Removes the instance of the characters matching.
			for(int index3=index2;index3<lengthSecondWord;index3++)
		{
			//This is just replacing the character that was matched.
			temp[index3] = temp[index3+1];
		}
			//Reduces the size of the second word.
			//lengthSecondWord--;

			//Since the character was found in the second word. Goes to the next character.
			continue;
		}
	}

	index1++;
}
}

	else
{

	//Since the words are not the same length they cannot be anagrams.
	cout << " The length of the words are not equal. They are not an anagram." <<
	endl << endl;
	return firstWord;
}
	
		

	//Test to see if the test condition is false. Indicating that a character 
	//was not found.
	if (testAnagram == false)
{
	//If the test condition was false then the wards are not anagrams.
	cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
	"! " << endl << endl;
}
	else
{
	//If the test condition was true then the words are anagrams.
	cout << " " << firstWord << " and " << secondWord << " are anagrams! "
	<< endl;
}

	return firstWord;
}

Are there any words that do work?

I think the problem is here

#
//Removes the instance of the characters matching.
#
for(int index3=index2;index3<lengthSecondWord;index3++)

You're removing every single instance of the character at index1 from the second word. You only want to remove one instance of the character if I'm not mistaken, the instance of the character at index2.

the other words work it just those two. I even created a different file to test the program because I know that the professor will use some words that are very close to being anagrams to see if it will fool the program.

Your code is extremely hard to read due to the seeming random indentation. If you indent consistently, it's easy for you and us to read. Visual Studio Express will format code properly with a few keystrokes. It's almost impossible to tell where a loop or an if statement starts and ends and what is nested inside what the way you have it formatted.

i hope this makes it easier to read

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

string anagram(const string& firstWord, const string& secondWord);

int main()
{
	string firstWord;			// First word in the file 
	string secondWord;			// Second word in the file

	//opens data file
	ifstream in("filename.txt");
	// tell the computer to print out an error message if not in file
	if(!in.is_open())
	{
		cout << "Can't open input file\n";
	}
	// tell the program that while in the file continue to run 
	while( in >> firstWord >> secondWord)
	{
		cout << " " << firstWord << " " << secondWord << endl;
		anagram(firstWord, secondWord);
		cout << endl << " Press Enter to continue " << endl;
		cin.get();
	}
	in.clear();
	in.close();
	// print a closing message and exit program 
	cout << endl << "-=- Execution Terminated -=-" << endl << endl;
	cout << "==================================================" << endl 
		<< endl;

	return 0;
}
//This function tests two inputted words to see if they are anagrams.
string anagram(const string& firstWord, const string& secondWord)
{
	//This gets the length of the first string.
	int lengthFirstWord = firstWord.length();
	
	//This gets the length of the second string.
	int lengthSecondWord = secondWord.length();
	
	//This sets the test to true to start the testing loops.
	bool testAnagram = true;
	
	//This is copying the second word for testing.
	string temp(secondWord);

	//These output the length of the strings.
	cout << " The length of the first word is: " << lengthFirstWord << endl;
	cout << " The length of the second word is: " << lengthSecondWord << endl;

	//The if statement test to see if the length of the words are equal.
	//If they are not then they are not anagrams.
	if (lengthFirstWord == lengthSecondWord)
	{
		int index1 = 0;
		
		//This starts the testing by getting the first character.
		while( index1 < lengthFirstWord && testAnagram != false)
		{
			//Takes one character and puts it in 1 character string.
			string testingChar = firstWord.substr(index1,1);
			
			//This sets the the tester to false. This will end the first loop if the
			//character is not found.
			testAnagram = false;
			
			//This tests a character from the first string against the characters of
			//the second string.
			for (int index2 = 0;index2 < lengthSecondWord; index2++)
				
				//Takes one character and puts it in 1 character string.
				string testedChar = temp.substr (index2,1);
			
			//Test if the characters match.
			if (testingChar == testedChar)
			{
				//Changes the test condition to true to continue test the rest of the word
				//string.
				testAnagram = true;
				
				//Removes the instance of the characters matching.
				for(int index3=index2;index3<lengthSecondWord;index3++)
				{
					//This is just replacing the character that was matched.
					temp[index3] = temp[index3+1];
				}
				
				//Reduces the size of the second word.
				//lengthSecondWord--;
				
				//Since the character was found in the second word. Goes to the next character.
				continue;
			}
		}
		
		index1++;
	}
}
else
{
	//Since the words are not the same length they cannot be anagrams.
	cout << " The length of the words are not equal. They are not an anagram." <<
	endl << endl;
	return firstWord;
}
//Test to see if the test condition is false. Indicating that a character 
//was not found.
if (testAnagram == false)
{
	//If the test condition was false then the wards are not anagrams.
	cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
	"! " << endl << endl;
}
else
{
	//If the test condition was true then the words are anagrams.
	cout << " " << firstWord << " and " << secondWord << " are anagrams! "
	<< endl;
}
return firstWord;
}

Yes, that's much better, and if you format it using Visual Studio, line 106 immediately stands out as being unindented and thus not part of any function. It's the word "else" and thus NEEDS to be INSIDE a function, and in particular it should be following an if block or else-if block. The fact that it is all the way to the left tells you that it is not inside of any brackets and so you have a brackets problem. Many editors (and Visual Studio probably does too, but apparently I don't have this option turned on) have an option where you can click on a bracket and it will find its matching bracket. Very useful. So your brackets, at minimum, do not match each other. You need to go through your code and match brackets and make sure that everything you intend to be inside brackets pairs actually is. That's one reason why good indentation is so essential. It immediately makes these brackets stand out. Everything in line 106 and beyond is currently not in any function so you have at least one extra ending bracket before then. My guess is that line 106 is supposed to be the "else" attached to the if-statement from line 60 and thus line 105 should be deleted.

ok I aedome more words to my list and it takes computer and computer as not being anagrams but impression and permission which are anagrams and says that they are not. the code below is the updated code. can someone help?

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

string anagram(const string& firstWord, const string& secondWord);

int main()
{
	string firstWord;			// First word in the file 
	string secondWord;			// Second word in the file
	
	
	//opens data file
    ifstream in("anagrams2.txt");

    // tell the computer to print out an error message if not in file
	if(!in.is_open())
	{
		cout << "Can't open input file\n";
	}

	// tell the program that while in the file continue to run 
    while( in >> firstWord >> secondWord)
	{
		cout << " " << firstWord << " " << secondWord << endl;
		anagram(firstWord, secondWord);
		cout << endl << " Press Enter to continue " << endl;
		cin.get();
	}

		in.clear();
		in.close();
		// print a closing message and exit program 
		cout << endl << "-=- Execution Terminated -=-" << endl << endl;
		cout << "==================================================" << endl 
         << endl;

    return 0;
}

//This function tests two inputted words to see if they are anagrams.

string anagram(const string& firstWord, const string& secondWord)
{
	//This gets the length of the first string.
	int lengthFirstWord = firstWord.length();
	
	//This gets the length of the second string.
	int lengthSecondWord = secondWord.length();

	//This sets the test to true to start the testing loops.
	bool testAnagram = true;

	//This is copying the second word for testing.
	string temp(secondWord);

	//These output the length of the strings.
	cout << " The length of the first word is: " << lengthFirstWord << endl;
	cout << " The length of the second word is: " << lengthSecondWord << endl;

	//The if statement test to see if the length of the words are equal.
	//If they are not then they are not anagrams.
	if (lengthFirstWord == lengthSecondWord)
	{
		int index1 = 0;

		//This starts the testing by getting the first character.
		while( index1 < lengthFirstWord && testAnagram != false)
		{
			//Takes one character and puts it in 1 character string.
			string testingChar = firstWord.substr(index1,1);

			//This sets the the tester to false. This will end the first loop if the
			//character is not found.
			testAnagram = false;

	//This tests a character from the first string against the characters of
	//the second string.
	for (int index2 = 0;index2 < lengthSecondWord; index2++)
	{
		//Takes one character and puts it in 1 character string.
		string testedChar = temp.substr (index2,1);

		//Test if the characters match.
		if (testingChar == testedChar)
		{
			//Changes the test condition to true to continue test the rest of the word
			//string.
			testAnagram = true;

			//Removes the instance of the characters matching.
			for(int index3=index2;index3<lengthSecondWord;index3++)
		{
			//This is just replacing the character that was matched.
			temp[index3] = temp[index3+1];
		}
			//Reduces the size of the second word.
			//lengthSecondWord--;

			//Since the character was found in the second word. Goes to the next character.
			continue;
		}
	}

	index1++;
}
}

	else
{

	//Since the words are not the same length they cannot be anagrams.
	cout << " The length of the words are not equal. They are not an anagram." <<
	endl << endl;
	return firstWord;
}
	
		

	//Test to see if the test condition is false. Indicating that a character 
	//was not found.
	if ((testAnagram == false)|| (firstWord == secondWord))
{
	//If the test condition was false then the wards are not anagrams.
	cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
	"! " << endl << endl;
}
	else
{
	//If the test condition was true then the words are anagrams.
	cout << " " << firstWord << " and " << secondWord << " are anagrams! "
	<< endl;
}

	return firstWord;
}
This article has been dead for over six months. Start a new discussion instead.