We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Anagrams

edelossantos
edelossantos asked
on
Medium Priority
296 Views
Last Modified: 2010-04-01

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)
Comment
Watch Question

Commented:
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;

};

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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");

}
>>>> 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
Commented:
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);

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

Author

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
Del, I didn't want to blame you. Just wondered.

Thanks for the points ;-)

Regards, Alex
 
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.