Arg! OOP and strings/char arrays!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Arg! OOP and strings/char arrays!

Hybrid View

  1. #1
    prototype Guest

    Arg! OOP and strings/char arrays!


    This seemed pretty simple to me.

    #include <iostream.h>
    #include <string.h>

    class thecounter{
    private: static int maxlen;
    char input[maxlen];

    public: thecounter(int c)
    {maxlen=c}
    void setText()
    {
    do{
    cout<<"Enter whatever, as long as it is no more than ";
    cout<<maxlen<<" characters including spaces.\n";
    cin.getline(input,(--maxlen));
    if(int check = (strlen(input)>(--maxlen)){
    cout<<"Error! I said no more than "<<maxlen<<"!";
    cout<<"Re-enter text.";
    cin.getline(input,(--maxlen));
    }
    } while(check !=0);
    }
    static int NumberOfSentances;
    static int NumberOfWords;
    static int NumberOfLetters;
    void textCount()
    {
    for(int i=0;i<maxlen;i++){
    if(isalnum(input[i]))
    NumberOfLetters++;
    if(isspace(input[i]))
    NumberOfWords++;
    if(ispunct(input[i]))
    NumberOfSentances;
    }
    }
    void show()
    {
    cout<<"There are "<<NumberOfSentances<<"sentances."
    cout<<"There are "<<NumberOfWords<<"words."
    cout<<"There are "<<NumberOfLetters<<"letters."
    }
    };

    void main(){
    thecounter first=100,second=200,third=300;
    first.setText();
    first.textCount();
    first.show();
    second.setText();
    second.textCount();
    second.show();
    third.setText();
    third.textcount();
    third.show();
    }

    All I want to do, is have a class that can
    count the number of letters, words, and sentances
    in a text entered by the user. I used some macros
    to figure out whats what. But I am getting about 12
    errors, and I don`t know what they mean. It says
    that the varible "maxlen" should be a const int, but
    then I can`t use different length text strings. Any
    ideas?

  2. #2
    Simon Sellick Guest

    Re: Arg! OOP and strings/char arrays!


    "prototype" <prototype@myself.com> wrote:
    >
    >This seemed pretty simple to me.
    >
    >#include <iostream.h>
    >#include <string.h>
    >
    >class thecounter{
    > private: static int maxlen;
    > char input[maxlen];
    >
    > public: thecounter(int c)
    > {maxlen=c}
    > void setText()
    > {
    > do{
    > cout<<"Enter whatever, as long as it is no more than ";
    > cout<<maxlen<<" characters including spaces.\n";
    > cin.getline(input,(--maxlen));
    > if(int check = (strlen(input)>(--maxlen)){
    > cout<<"Error! I said no more than "<<maxlen<<"!";
    > cout<<"Re-enter text.";
    > cin.getline(input,(--maxlen));
    > }
    > } while(check !=0);
    > }
    > static int NumberOfSentances;
    > static int NumberOfWords;
    > static int NumberOfLetters;
    > void textCount()
    > {
    > for(int i=0;i<maxlen;i++){
    > if(isalnum(input[i]))
    > NumberOfLetters++;
    > if(isspace(input[i]))
    > NumberOfWords++;
    > if(ispunct(input[i]))
    > NumberOfSentances;
    > }
    > }
    > void show()
    > {
    > cout<<"There are "<<NumberOfSentances<<"sentances."
    > cout<<"There are "<<NumberOfWords<<"words."
    > cout<<"There are "<<NumberOfLetters<<"letters."
    > }
    >};
    >
    >void main(){
    > thecounter first=100,second=200,third=300;
    > first.setText();
    > first.textCount();
    > first.show();
    > second.setText();
    > second.textCount();
    > second.show();
    > third.setText();
    > third.textcount();
    > third.show();
    >}
    >
    >All I want to do, is have a class that can
    >count the number of letters, words, and sentances
    >in a text entered by the user. I used some macros
    >to figure out whats what. But I am getting about 12
    >errors, and I don`t know what they mean. It says
    >that the varible "maxlen" should be a const int, but
    >then I can`t use different length text strings. Any
    >ideas?


    Doesn't 'static' mean class-level? I would have expected maxlen to be an
    instance variable, associated with one particular thecounter. (If you want
    a default value, you can assign it one in the constructor)
    Hope this helps.

  3. #3
    prototype Guest

    Re: Arg! OOP and strings/char arrays!


    "Simon Sellick" <simon.sellick@tesco.net> wrote:
    >
    >
    >Doesn't 'static' mean class-level? I would have expected maxlen to be an
    >instance variable, associated with one particular thecounter. (If you want
    >a default value, you can assign it one in the constructor)
    >Hope this helps.


    Thats the rub. I need maxlen to be different for each object. But my
    compiler (Borland C++ 4.52) insists that maxlen be const. That is what I

    can`t figure out.

  4. #4
    Danny Kalev Guest

    Re: Arg! OOP and strings/char arrays!



    prototype wrote:
    >
    > "Simon Sellick" <simon.sellick@tesco.net> wrote:
    > >
    > >
    > >Doesn't 'static' mean class-level? I would have expected maxlen to be an
    > >instance variable, associated with one particular thecounter. (If you want
    > >a default value, you can assign it one in the constructor)
    > >Hope this helps.

    >
    > Thats the rub. I need maxlen to be different for each object. But my
    > compiler (Borland C++ 4.52) insists that maxlen be const.

    And rightfully so:

    char input[maxlen];

    You're using maxlen as an array's size. An array's size must be a
    constant. You have to decide whether you want it as a constant and in
    that case, it shouldn't be a class member, you can use an external
    constant. If you want a distinct variable for every object, you will
    have to allocate the array input dynamically using new (and don't
    forget to delete[] it in the destructor of course).

    Danny

    That is what I
    >
    > can`t figure out.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center