i have no idea why this does not work

This is a discussion on i have no idea why this does not work within the C++ Programming forums, part of the General Programming Boards category; I am making a program that will analize text and determine if it is an anagram. all the inputs are ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    i have no idea why this does not work

    I am making a program that will analize text and determine if it is an anagram.
    all the inputs are working, but
    Code:
    compareOccurences();
    always returns true.
    please help.
    thanks in advance.
    p.s. i am relatively new to c++, so sorry if it is a stupid mistake
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    int listOfOccurence [28];
    int listOfOccurence2 [28];
    char plainText [500];
    char anagram [500];
    
    
    
    void getOccurences ()
    {
         int count=0;
         while (count < 500)
         {
               switch (anagram[count])
               {
                      case 'a'|'A':
                      {
                           listOfOccurence[1]+=1;
                           break;
                      }
                      case 'b'|'B':
                      {
                           listOfOccurence[2]+=1;
                           break;
                      }
                      case 'c'|'C':
                      {
                           listOfOccurence[3]+=1;
                           break;
                      }
                      case 'd'|'D':
                      {
                           listOfOccurence[4]+=1;
                           break;
                      }
                      case 'e'|'E':
                      {
                           listOfOccurence[5]+=1;
                           break;
                      }
                      case 'f'|'F':
                      {
                           listOfOccurence[6]+=1;
                           break;
                      }
                      case 'g'|'G':
                      {
                           listOfOccurence[7]+=1;
                           break;
                      }
                      case 'h'|'H':
                      {
                           listOfOccurence[8]+=1;
                           break;
                      }
                      case'i'|'I':
                      {
                           listOfOccurence[9]+=1;
                           break;
                      }
                      case'j'|'J':
                      {
                           listOfOccurence[10]+=1;
                           break;
                      }
                      case'k'|'K':
                      {
                           listOfOccurence[11]+=1;
                           break;
                      }
                      case'l'|'L':
                      {
                           listOfOccurence[12]+=1;
                           break;
                      }
                      case'm'|'M':
                      {
                           listOfOccurence[13]+=1;
                           break;
                      }
                      case'n'|'N':
                      {
                           listOfOccurence[14]+=1;
                           break;
                      }
                      case'o'|'O':
                      {
                           listOfOccurence[15]+=1;
                           break;
                      }
                      case'p'|'P':
                      {
                           listOfOccurence[16]+=1;
                           break;
                      }
                      case'q'|'Q':
                      {
                           listOfOccurence[17]+=1;
                           break;
                      }
                      case'r'|'R':
                      {
                           listOfOccurence[18]+=1;
                           break;
                      }
                      case's'|'S':
                      {
                           listOfOccurence[19]+=1;
                           break;
                      }
                      case't'|'T':
                      {
                           listOfOccurence[20]+=1;
                           break;
                      }
                      case'u'|'U':
                      {
                           listOfOccurence[21]+=1;
                           break;
                      }
                      case'v'|'V':
                      {
                           listOfOccurence[22]+=1;
                           break;
                      }
                      case'w'|'W':
                      {
                           listOfOccurence[23]+=1;
                           break;
                      }
                      case'x'|'X':
                      {
                           listOfOccurence[24]+=1;
                           break;
                      }
                      case'y'|'Y':
                      {
                           listOfOccurence[25]+=1;
                           break;
                      }
                      case'z'|'Z':
                      {
                           listOfOccurence[26]+=1;
                           break;
                      }
                      case' ':
                      {
                           listOfOccurence[27]+=1;
                           break;
                      }
               }
               count ++;
         }
         count =0;
         while (count <500)
         {
               switch (anagram[count])
               {
                      case 'a'|'A':
                      {
                           listOfOccurence2[1]+=1;
                           break;
                      }
                      case 'b'|'B':
                      {
                           listOfOccurence2[2]+=1;
                           break;
                      }
                      case 'c'|'C':
                      {
                           listOfOccurence2[3]+=1;
                           break;
                      }
                      case 'd'|'D':
                      {
                           listOfOccurence2[4]+=1;
                           break;
                      }
                      case 'e'|'E':
                      {
                           listOfOccurence2[5]+=1;
                           break;
                      }
                      case 'f'|'F':
                      {
                           listOfOccurence2[6]+=1;
                           break;
                      }
                      case 'g'|'G':
                      {
                           listOfOccurence2[7]+=1;
                           break;
                      }
                      case 'h'|'H':
                      {
                           listOfOccurence2[8]+=1;
                           break;
                      }
                      case'i'|'I':
                      {
                           listOfOccurence2[9]+=1;
                           break;
                      }
                      case'j'|'J':
                      {
                           listOfOccurence2[10]+=1;
                           break;
                      }
                      case'k'|'K':
                      {
                           listOfOccurence2[11]+=1;
                           break;
                      }
                      case'l'|'L':
                      {
                           listOfOccurence2[12]+=1;
                           break;
                      }
                      case'm'|'M':
                      {
                           listOfOccurence2[13]+=1;
                           break;
                      }
                      case'n'|'N':
                      {
                           listOfOccurence2[14]+=1;
                           break;
                      }
                      case'o'|'O':
                      {
                           listOfOccurence2[15]+=1;
                           break;
                      }
                      case'p'|'P':
                      {
                           listOfOccurence2[16]+=1;
                           break;
                      }
                      case'q'|'Q':
                      {
                           listOfOccurence2[17]+=1;
                           break;
                      }
                      case'r'|'R':
                      {
                           listOfOccurence2[18]+=1;
                           break;
                      }
                      case's'|'S':
                      {
                           listOfOccurence2[19]+=1;
                           break;
                      }
                      case't'|'T':
                      {
                           listOfOccurence2[20]+=1;
                           break;
                      }
                      case'u'|'U':
                      {
                           listOfOccurence2[21]+=1;
                           break;
                      }
                      case'v'|'V':
                      {
                           listOfOccurence2[22]+=1;
                           break;
                      }
                      case'w'|'W':
                      {
                           listOfOccurence2[23]+=1;
                           break;
                      }
                      case'x'|'X':
                      {
                           listOfOccurence2[24]+=1;
                           break;
                      }
                      case'y'|'Y':
                      {
                           listOfOccurence2[25]+=1;
                           break;
                      }
                      case'z'|'Z':
                      {
                           listOfOccurence2[26]+=1;
                           break;
                      }
                      case' ':
                      {
                           listOfOccurence2[27]+=1;
                           break;
                      }
               }
    
               count ++;
         }
         
    }
    bool compareOccurences()
    {
         int a;
         while (a<28)
         {
               if(listOfOccurence[a]==listOfOccurence2[a])
               a++;
               else if(listOfOccurence[a]!=listOfOccurence2[a])
                    return false;
         }
        
    }
    
    int main(int argc, char *argv[])
    {
        cout<<"Please enter the anagram, including spaces: \n";
        cin.getline(anagram, 500);
        cout<<"Pleaase enter the plain text, including spaces: \n";
        cin.getline(plainText, 500);
    
        getOccurences();
    
        
        if(compareOccurences)
            cout<<"The text you have entered is an anagram\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    For starters, use std::string to simplify string handling. Avoid global variables.

    The expression 'a'|'A' performs a bitwise or of 'a' and 'A', which is probably not what you want. What you probably want is:
    Code:
    case 'a':
    case 'A':
    which is effectively
    Code:
    if (anagram[count] == 'a' || anagram[count] == 'A')
    Note also that compareOccurences() never returns true. You probably want it to return true when the loop terminates. Oh, and you should initialise a to 1, and pass listOfOccurence as an argument instead of using it as a global variable.

    In fact, std::map could come in handy here (you are pretty much mapping chars to their frequency and then comparing the two mappings to see if they match), but I guess that you want to try your hand at implementing your algorithm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  2. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM
  3. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM
  4. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21