[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Anagrams

Posted on 2006-04-05
7
Medium Priority
?
280 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)
0
Comment
Question by:edelossantos
7 Comments
 
LVL 17

Assisted Solution

by:HuyBD
HuyBD earned 300 total points
ID: 16389295
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 1400 total points
ID: 16389299
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
ID: 16389302
>>>> 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 1400 total points
ID: 16389307
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16389367
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
 

Author Comment

by:edelossantos
ID: 16389384
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16389512
Del, I didn't want to blame you. Just wondered.

Thanks for the points ;-)

Regards, Alex
 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question