Anagrams


Does not compile, please advise.  Del

class Ana {

public:

      Ana(){};
      ~Ana(){};
      int getArraySize(char []);
      void printAnagrams ( char [], int );

private:

      char str[256];
      char *nWord;
      int Counter = 0;

};

#endif

*******************


// ana.cpp
// Sheet 2

#include<iostream>
#include<cctype>
#include<string>
#include "ana.h"

using namespace std;

int Ana::getArraySize(char a[]) {
 
  int sz = 0;
  int x = 0;

  do {
   
      x = a[sz];
      sz++;
  }
  while(x != 0);
    sz-=1;

  return (sz);

}

void Ana::printAnagrams ( char a[], int len ) {

  if(len <= 1) {
   
    nWord = a;
    cout << nWord << endl;
    Counter++;
     
  }
  else {
   
    int i;
    char temp;

    for (i = 0; i < len; i++) {
     
      temp = a[i];
      a[i] = a[len-1];
      a[len-1] = temp;

      printAnagrams(a, len-1);

      temp = a[i];
      a[i] = a[len-1];
      a[len-1] = temp;
     
    }
   
  }

}
#include "ana.h"
int main() {

  Ana = a;
 
  cout <<"Enter Word: ";
  cin >> str;
  cout << endl;
  int Len = getArraySize(str);

  a.printAnagrams(str,Len);
  cout << endl << Counter <<" Anagrams";
  cout<< endl;

  return 0;
  system("pause");

}

Output:

In file included from ana.cpp:8:
ana.h:29: error: ISO C++ forbids initialization of member `Counter'
ana.h:29: error: making `Counter' static
ana.h:29: error: ISO C++ forbids in-class initialization of non-const static
   member `Counter'
ana.cpp: In function `int main()':
ana.cpp:63: error: parse error before `=' token
ana.cpp:66: error: `str' undeclared (first use this function)
ana.cpp:66: error: (Each undeclared identifier is reported only once for each
   function it appears in.)
ana.cpp:68: error: `getArraySize' undeclared (first use this function)
ana.cpp:70: error: `a' undeclared (first use this function)
ana.cpp:71: error: `Counter' undeclared (first use this function)
edelossantosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuyBDCommented:
yeah!
compiler doen't alow initial for a member of class, you must initial Counter in constructor or other class function
e.g

class Ana {

public:

     Ana(){Counter=0;};
     ~Ana(){};
     int getArraySize(char []);
     void printAnagrams ( char [], int );
private:

     char str[256];
     char *nWord;
     int Counter;

};
0
AlexFMCommented:
class Ana {

public:

//    Ana(){};
    Ana() : Counter(0)
    {

    };

    ~Ana(){};
    int getArraySize(char []);
//    void printAnagrams ( char [], int );
    void printAnagrams ( char*, int );

    int Counter;   // moved


private:

    char str[256];
    char *nWord;
//    int Counter = 0;

};


#include<iostream>
#include<cctype>
#include<string>
#include "ana.h"

using namespace std;

int Ana::getArraySize(char a[]) {

    int sz = 0;
    int x = 0;

    do {

        x = a[sz];
        sz++;
    }
    while(x != 0);
    sz-=1;

    return (sz);

}

//void Ana::printAnagrams ( char a[], int len ) {
void Ana::printAnagrams ( char* a, int len ) {

    if(len <= 1) {

        nWord = a;
        cout << nWord << endl;
        Counter++;

    }
    else {

        int i;
        char temp;

        for (i = 0; i < len; i++) {

            temp = a[i];
            a[i] = a[len-1];
            a[len-1] = temp;

            printAnagrams(a, len-1);

            temp = a[i];
            a[i] = a[len-1];
            a[len-1] = temp;

        }

    }

}

int main() {

//    Ana = a;
    Ana a;

    char str[100];     // added

    cout <<"Enter Word: ";
    cin >> str;
    cout << endl;
//    int Len = getArraySize(str);
    int Len = (int)strlen(str);

//    a.printAnagrams(str,Len);
    a.printAnagrams(str,Len);
//    cout << endl << Counter <<" Anagrams";
    cout << endl << a.Counter <<" Anagrams";
    cout<< endl;

    return 0;
    system("pause");

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
>>>> Counter = 0;

You may not init the class member in the class definition but in the constructor;

   Ana::Ana()
   : nWord(NULL), Counter(0)
   {
       str[0] = '\0';
   }

>>>> Ana = a;

Seems you mean

   Ana a;

Regards, Alex
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

AlexFMCommented:
I replaced getArraySize with strLen, possibly this is not what you mean. Maybe you need this:

//int Len = getArraySize(str);
int Len = a.getArraySize(str);

0
itsmeandnobodyelseCommented:
Del,

you are an EE member since 2 years. In all the time you have seen hundreds of C++ samples and should know the very basics of C++. The errors you posted above easily can be solved by a beginner. The compiler showed you the lines that were wrong and the error messages were quite clearly.

I wonder why you need to post a 500 points question for something like that.

Regards, Alex
0
edelossantosAuthor Commented:
Alex,

When I made the mistake of taking a quarter off from CS, I forgot alot of the simple concepts.  I am playing catchup, and I can see now that taking any time off from progamming is a big mistake.  Del
0
itsmeandnobodyelseCommented:
Del, I didn't want to blame you. Just wondered.

Thanks for the points ;-)

Regards, Alex
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.