943,840 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 747
  • C++ RSS
Jul 29th, 2008
0

Problem with n array

Expand Post »
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?
c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. string anagram(const string& firstWord, const string& secondWord);
  10.  
  11. int main()
  12. {
  13. string firstWord; // First word in the file
  14. string secondWord; // Second word in the file
  15.  
  16.  
  17. //opens data file
  18. ifstream in("filename.txt");
  19.  
  20. // tell the computer to print out an error message if not in file
  21. if(!in.is_open())
  22. {
  23. cout << "Can't open input file\n";
  24. }
  25.  
  26. // tell the program that while in the file continue to run
  27. while( in >> firstWord >> secondWord)
  28. {
  29. cout << " " << firstWord << " " << secondWord << endl;
  30. anagram(firstWord, secondWord);
  31. cout << endl << " Press Enter to continue " << endl;
  32. cin.get();
  33. }
  34.  
  35. in.clear();
  36. in.close();
  37. // print a closing message and exit program
  38. cout << endl << "-=- Execution Terminated -=-" << endl << endl;
  39. cout << "==================================================" << endl
  40. << endl;
  41.  
  42. return 0;
  43. }
  44.  
  45. //This function tests two inputted words to see if they are anagrams.
  46.  
  47. string anagram(const string& firstWord, const string& secondWord)
  48. {
  49. //This gets the length of the first string.
  50. int lengthFirstWord = firstWord.length();
  51.  
  52. //This gets the length of the second string.
  53. int lengthSecondWord = secondWord.length();
  54.  
  55. //This sets the test to true to start the testing loops.
  56. bool testAnagram = true;
  57.  
  58. //This is copying the second word for testing.
  59. string temp(secondWord);
  60.  
  61. //These output the length of the strings.
  62. cout << " The length of the first word is: " << lengthFirstWord << endl;
  63. cout << " The length of the second word is: " << lengthSecondWord << endl;
  64.  
  65. //The if statement test to see if the length of the words are equal.
  66. //If they are not then they are not anagrams.
  67. if (lengthFirstWord == lengthSecondWord)
  68. {
  69. int index1 = 0;
  70.  
  71. //This starts the testing by getting the first character.
  72. while( index1 < lengthFirstWord && testAnagram != false)
  73. {
  74. //Takes one character and puts it in 1 character string.
  75. string testingChar = firstWord.substr(index1,1);
  76.  
  77. //This sets the the tester to false. This will end the first loop if the
  78. //character is not found.
  79. testAnagram = false;
  80.  
  81. //This tests a character from the first string against the characters of
  82. //the second string.
  83. for (int index2 = 0;index2 < lengthSecondWord; index2++)
  84. {
  85. //Takes one character and puts it in 1 character string.
  86. string testedChar = temp.substr (index2,1);
  87.  
  88. //Test if the characters match.
  89. if (testingChar == testedChar)
  90. {
  91. //Changes the test condition to true to continue test the rest of the word
  92. //string.
  93. testAnagram = true;
  94.  
  95. //Removes the instance of the characters matching.
  96. for(int index3=index2;index3<lengthSecondWord;index3++)
  97. {
  98. //This is just replacing the character that was matched.
  99. temp[index3] = temp[index3+1];
  100. }
  101. //Reduces the size of the second word.
  102. //lengthSecondWord--;
  103.  
  104. //Since the character was found in the second word. Goes to the next character.
  105. continue;
  106. }
  107. }
  108.  
  109. index1++;
  110. }
  111. }
  112.  
  113. else
  114. {
  115.  
  116. //Since the words are not the same length they cannot be anagrams.
  117. cout << " The length of the words are not equal. They are not an anagram." <<
  118. endl << endl;
  119. return firstWord;
  120. }
  121.  
  122.  
  123.  
  124. //Test to see if the test condition is false. Indicating that a character
  125. //was not found.
  126. if (testAnagram == false)
  127. {
  128. //If the test condition was false then the wards are not anagrams.
  129. cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
  130. "! " << endl << endl;
  131. }
  132. else
  133. {
  134. //If the test condition was true then the words are anagrams.
  135. cout << " " << firstWord << " and " << secondWord << " are anagrams! "
  136. << endl;
  137. }
  138.  
  139. return firstWord;
  140. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 29th, 2008
0

Re: Problem with n array

What are the words that don't work?
Reputation Points: 30
Solved Threads: 0
Junior Poster in Training
Kob0724 is offline Offline
52 posts
since Jul 2005
Jul 29th, 2008
0

Re: Problem with n array

computer computer
bacterial calibrate
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 29th, 2008
0

Re: Problem with n array

Are there any words that do work?

I think the problem is here
C++ Syntax (Toggle Plain Text)
  1. #
  2. //Removes the instance of the characters matching.
  3. #
  4. 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.
Reputation Points: 30
Solved Threads: 0
Junior Poster in Training
Kob0724 is offline Offline
52 posts
since Jul 2005
Jul 29th, 2008
0

Re: Problem with n array

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 29th, 2008
0

Re: Problem with n array

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 29th, 2008
0

Re: Problem with n array

i hope this makes it easier to read
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. string anagram(const string& firstWord, const string& secondWord);
  9.  
  10. int main()
  11. {
  12. string firstWord; // First word in the file
  13. string secondWord; // Second word in the file
  14.  
  15. //opens data file
  16. ifstream in("filename.txt");
  17. // tell the computer to print out an error message if not in file
  18. if(!in.is_open())
  19. {
  20. cout << "Can't open input file\n";
  21. }
  22. // tell the program that while in the file continue to run
  23. while( in >> firstWord >> secondWord)
  24. {
  25. cout << " " << firstWord << " " << secondWord << endl;
  26. anagram(firstWord, secondWord);
  27. cout << endl << " Press Enter to continue " << endl;
  28. cin.get();
  29. }
  30. in.clear();
  31. in.close();
  32. // print a closing message and exit program
  33. cout << endl << "-=- Execution Terminated -=-" << endl << endl;
  34. cout << "==================================================" << endl
  35. << endl;
  36.  
  37. return 0;
  38. }
  39. //This function tests two inputted words to see if they are anagrams.
  40. string anagram(const string& firstWord, const string& secondWord)
  41. {
  42. //This gets the length of the first string.
  43. int lengthFirstWord = firstWord.length();
  44.  
  45. //This gets the length of the second string.
  46. int lengthSecondWord = secondWord.length();
  47.  
  48. //This sets the test to true to start the testing loops.
  49. bool testAnagram = true;
  50.  
  51. //This is copying the second word for testing.
  52. string temp(secondWord);
  53.  
  54. //These output the length of the strings.
  55. cout << " The length of the first word is: " << lengthFirstWord << endl;
  56. cout << " The length of the second word is: " << lengthSecondWord << endl;
  57.  
  58. //The if statement test to see if the length of the words are equal.
  59. //If they are not then they are not anagrams.
  60. if (lengthFirstWord == lengthSecondWord)
  61. {
  62. int index1 = 0;
  63.  
  64. //This starts the testing by getting the first character.
  65. while( index1 < lengthFirstWord && testAnagram != false)
  66. {
  67. //Takes one character and puts it in 1 character string.
  68. string testingChar = firstWord.substr(index1,1);
  69.  
  70. //This sets the the tester to false. This will end the first loop if the
  71. //character is not found.
  72. testAnagram = false;
  73.  
  74. //This tests a character from the first string against the characters of
  75. //the second string.
  76. for (int index2 = 0;index2 < lengthSecondWord; index2++)
  77.  
  78. //Takes one character and puts it in 1 character string.
  79. string testedChar = temp.substr (index2,1);
  80.  
  81. //Test if the characters match.
  82. if (testingChar == testedChar)
  83. {
  84. //Changes the test condition to true to continue test the rest of the word
  85. //string.
  86. testAnagram = true;
  87.  
  88. //Removes the instance of the characters matching.
  89. for(int index3=index2;index3<lengthSecondWord;index3++)
  90. {
  91. //This is just replacing the character that was matched.
  92. temp[index3] = temp[index3+1];
  93. }
  94.  
  95. //Reduces the size of the second word.
  96. //lengthSecondWord--;
  97.  
  98. //Since the character was found in the second word. Goes to the next character.
  99. continue;
  100. }
  101. }
  102.  
  103. index1++;
  104. }
  105. }
  106. else
  107. {
  108. //Since the words are not the same length they cannot be anagrams.
  109. cout << " The length of the words are not equal. They are not an anagram." <<
  110. endl << endl;
  111. return firstWord;
  112. }
  113. //Test to see if the test condition is false. Indicating that a character
  114. //was not found.
  115. if (testAnagram == false)
  116. {
  117. //If the test condition was false then the wards are not anagrams.
  118. cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
  119. "! " << endl << endl;
  120. }
  121. else
  122. {
  123. //If the test condition was true then the words are anagrams.
  124. cout << " " << firstWord << " and " << secondWord << " are anagrams! "
  125. << endl;
  126. }
  127. return firstWord;
  128. }
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 29th, 2008
0

Re: Problem with n array

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.
Last edited by VernonDozier; Jul 29th, 2008 at 11:58 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 30th, 2008
0

Re: Problem with n array

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?
c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. string anagram(const string& firstWord, const string& secondWord);
  10.  
  11. int main()
  12. {
  13. string firstWord; // First word in the file
  14. string secondWord; // Second word in the file
  15.  
  16.  
  17. //opens data file
  18. ifstream in("anagrams2.txt");
  19.  
  20. // tell the computer to print out an error message if not in file
  21. if(!in.is_open())
  22. {
  23. cout << "Can't open input file\n";
  24. }
  25.  
  26. // tell the program that while in the file continue to run
  27. while( in >> firstWord >> secondWord)
  28. {
  29. cout << " " << firstWord << " " << secondWord << endl;
  30. anagram(firstWord, secondWord);
  31. cout << endl << " Press Enter to continue " << endl;
  32. cin.get();
  33. }
  34.  
  35. in.clear();
  36. in.close();
  37. // print a closing message and exit program
  38. cout << endl << "-=- Execution Terminated -=-" << endl << endl;
  39. cout << "==================================================" << endl
  40. << endl;
  41.  
  42. return 0;
  43. }
  44.  
  45. //This function tests two inputted words to see if they are anagrams.
  46.  
  47. string anagram(const string& firstWord, const string& secondWord)
  48. {
  49. //This gets the length of the first string.
  50. int lengthFirstWord = firstWord.length();
  51.  
  52. //This gets the length of the second string.
  53. int lengthSecondWord = secondWord.length();
  54.  
  55. //This sets the test to true to start the testing loops.
  56. bool testAnagram = true;
  57.  
  58. //This is copying the second word for testing.
  59. string temp(secondWord);
  60.  
  61. //These output the length of the strings.
  62. cout << " The length of the first word is: " << lengthFirstWord << endl;
  63. cout << " The length of the second word is: " << lengthSecondWord << endl;
  64.  
  65. //The if statement test to see if the length of the words are equal.
  66. //If they are not then they are not anagrams.
  67. if (lengthFirstWord == lengthSecondWord)
  68. {
  69. int index1 = 0;
  70.  
  71. //This starts the testing by getting the first character.
  72. while( index1 < lengthFirstWord && testAnagram != false)
  73. {
  74. //Takes one character and puts it in 1 character string.
  75. string testingChar = firstWord.substr(index1,1);
  76.  
  77. //This sets the the tester to false. This will end the first loop if the
  78. //character is not found.
  79. testAnagram = false;
  80.  
  81. //This tests a character from the first string against the characters of
  82. //the second string.
  83. for (int index2 = 0;index2 < lengthSecondWord; index2++)
  84. {
  85. //Takes one character and puts it in 1 character string.
  86. string testedChar = temp.substr (index2,1);
  87.  
  88. //Test if the characters match.
  89. if (testingChar == testedChar)
  90. {
  91. //Changes the test condition to true to continue test the rest of the word
  92. //string.
  93. testAnagram = true;
  94.  
  95. //Removes the instance of the characters matching.
  96. for(int index3=index2;index3<lengthSecondWord;index3++)
  97. {
  98. //This is just replacing the character that was matched.
  99. temp[index3] = temp[index3+1];
  100. }
  101. //Reduces the size of the second word.
  102. //lengthSecondWord--;
  103.  
  104. //Since the character was found in the second word. Goes to the next character.
  105. continue;
  106. }
  107. }
  108.  
  109. index1++;
  110. }
  111. }
  112.  
  113. else
  114. {
  115.  
  116. //Since the words are not the same length they cannot be anagrams.
  117. cout << " The length of the words are not equal. They are not an anagram." <<
  118. endl << endl;
  119. return firstWord;
  120. }
  121.  
  122.  
  123.  
  124. //Test to see if the test condition is false. Indicating that a character
  125. //was not found.
  126. if ((testAnagram == false)|| (firstWord == secondWord))
  127. {
  128. //If the test condition was false then the wards are not anagrams.
  129. cout << " " << firstWord << " and " << secondWord << " are not anagrams"<<
  130. "! " << endl << endl;
  131. }
  132. else
  133. {
  134. //If the test condition was true then the words are anagrams.
  135. cout << " " << firstWord << " and " << secondWord << " are anagrams! "
  136. << endl;
  137. }
  138.  
  139. return firstWord;
  140. }
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Jul 30th, 2008
0

Re: Problem with n array

The solution is a letter frequency counter.

http://www.daniweb.com/code/snippet906.html
Last edited by iamthwee; Jul 30th, 2008 at 4:36 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2337: 'count' : attribute not found
Next Thread in C++ Forum Timeline: simple thread question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC