I've been away from programming for a bit, and I never really learned pointers that well.
What I'm trying to do now is code from a model.
Here's a link to the model:
Click Here

My problem is that my output is not changing when I enter events.
The program is supposed to emulate a chameleon.
I just need help in understanding why it's not changing.
Thanks so much!
Here is my code:

// Assignment10.cpp : Defines the entry point for the console application.
//

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

using namespace std;

enum ChameleonState { COLD, WARM, ANGRY, ENRAPTURED };

const int tempLess = 1;
const int tempMore = 2;
const int predator = 3;
const int noPredator = 4;
const int mate = 5;
const int noMate = 6;


class Chameleon
{
public:
    Chameleon()
    {
        state;
        color;
        temperature;
        eventArr[tempLess] = &Chameleon::TempLess;
        eventArr[tempMore] = &Chameleon::TempMore;
        eventArr[predator] = &Chameleon::Predator;
        eventArr[noPredator] = &Chameleon::NoPredator;
        eventArr[mate] = &Chameleon::Mate;
        eventArr[noMate] = &Chameleon::NoMate;
    }
    void display()
    {

        string stateString = "";

        switch (state)
        {
        case ANGRY: stateString = " ANGRY "; break;
        case WARM: stateString = " WARM "; break;
        case COLD: stateString = " COLD "; break;
        case ENRAPTURED: stateString = " ENRAPTURED "; break;
        default:
            stateString = " COLD ";
            color = " Brown ";
            temperature = " <50 ";
        }
        cout << "************************************* " << endl;
        cout << endl;
        cout << " State is " << stateString << endl;
        cout << " Color is " << color << endl;
        cout << " Temperature is " << temperature << endl;
        cout << " \n ";

    }

    void processEvent(int event)
    {
        (this->*eventArr[event]) ();
        display();
    }

private:
    void TempLess()
    {
        if (state == COLD)
        {
            color = " Brown ";
            state = COLD;
        }
        else
            state = WARM;
        temperature = " >=50";
    }
    void TempMore()
    {
        if (state == WARM)
        {
            color = " Green ";
            state = WARM;
        }
        else
            state = COLD;
        temperature = " <50 ";
    }

    void Predator()
    {
        if (state == WARM)
        {
            state = ANGRY;
            temperature = " >=50 ";
            color = " Red ";
        }

    }
    void NoPredator()
    {
        if (state == ANGRY)
        {
            state = WARM;
            temperature = " >=50 ";
        }
        else
            state = WARM;
        temperature = " >=50 ";
        color = " Brown ";
    }



    void Mate()
    {
        if (state == WARM)
        {
            color = " Yellow ";
            state = ENRAPTURED;
            temperature = " <50 ";
        }


    }
    void NoMate()
    {
        if (state == ENRAPTURED)
        {
            state = COLD;
            temperature = " <50 ";
            color = " Yellow ";
        }
        else
            state = WARM;
        temperature = " >=50 ";


    }
    ChameleonState state;
    int temp;
    string color;
    string temperature;
    void(Chameleon::*eventArr[7]) ();
};

int main()
{
    Chameleon* s = new Chameleon();


    int event = 0;
    while (event != 10)
    {
        s->display();


        cout << "Chameleon events <pick one, 10 to quit>: " << endl;
        cout << "temp<50               1 " << endl;
        cout << "temp>=50     2 " << endl;
        cout << "predator present 3 " << endl;
        cout << "predator not present 4 " << endl;
        cout << "mate present     5 " << endl;
        cout << "mate not present 6 " << endl;
        cout << "Enter an event: ";
        cin >> event;

        cout << "\n\n\n";
    }

    if (event = 1)
        s->processEvent(COLD);
    else if (event = 2)
        s->processEvent(WARM);
    else if (event = 3)
        s->processEvent(ANGRY);
    else if (event = 4)
        s->processEvent(WARM);
    else if (event = 5)
        s->processEvent(ENRAPTURED);
    else if (event = 6)
        s->processEvent(WARM);
    else if (event = 10)
        return 0;








}

You may need to look and compare how you declare your function pointer here (at the end of the page -- Function Pointers Summary).

Quick question, what happen if a predator & a mate present? Which state would it present that event?

Edited 1 Year Ago by Taywin

That is an excellent question...I didn't design the model, just following it.
I actually have his book, Jumping into C++, but pointers are very confusing for me. So I declare my functions as say, void *TempLess(); instead of void TempLess();?
and then once it's declared where am I pointing to?

Yes, you need to declare a function similar to a pointer variable. You can point to it by its name. Then you can call it as a normal function. (Or you may keep everything as is but have to update how you declare your function).

Edited 1 Year Ago by Taywin

I would re-design ... bottom up and use table lookups and not use pointers at all.

Also, you seem to have redundant data ... for example ... temperature and temperature string ... but only need to store the temperature ...and then can lookup the other when needed.

I would love to avoid pointers altogether...however this professor is VERY specific on how he wants things done.(ie: Invoke event methods through an array of function pointers, etc.) He also gives an automatic 0 for non-compiles. I realize now of course that I didn't post his instructions, so I dropped the ball there. Honestly this was supposed to be a modeling class, specifically object-oriented analysis and design, and now at the end he wants us to code, rather unexpectedly I might add. Either way, I turned in what I had, but I would still like some more precise direction so I might understand it better the next time.

... would still like some more precise direction so I might understand it better the next time.

Then ... it would help if you could supply the complete program spec's ... and code.

The code I supplied IS the code...here are the instructions:
The Chameleon is an animal that can change color. Most people believe that the chameleon changes color to match its environment. Actually, his color is controlled primarily by temperature and emotional state. Examine the state diagram and then write a C++ program that models the diagram.

a.  'Design a Chameleon class'. sorry, the page wants this as code.

b. Write a class method to process each type of event. Note that in the diagram temp is both an event and a guard variable.
c. Invoke event methods through an array of function pointers.
d. Use function main as a driver. Prompt the console for event number.
e. After processing each event, output the current state, color, and temperature of the chameleon.

Submissions that do not compile will receive a grade of 0.
Submissions that are function correctly, but do not follow the design guidelines will receive a grade of 85.

... Examine the state diagram and then write a C++ program that models the diagram.

The diagram ?

I linked it in the first post. It's a link...I didn't see where I could insert a picture but I guess the attach button does it.
Here:
123.jpg

Thanks ... (sorry I missed seeing the link) ... I'll take a look and get back as soon as I can.

Ok I finally had time to look at your code ...

You stated:

... I just need help in understanding why it's not changing ...

So ... the code below may help you to see ... 'why it's not changing' ...
(I did not at all attempt to implement your 'state diagram'.)

// Assignment10.cpp //

#include <iostream>
#include <string>

using namespace std;

enum ChameleonState { COLD, WARM, ANGRY, ENRAPTURED };
const string STATES[] = { "COLD", "WARM", "ANGRY", "ENRAPTURE" };


enum TempState { low, high };
const string TEMPS[] = { "<50 ", ">=50 " };

enum ColourState { brown, yellow, green, red };
const string COLOURS[] = { "brown", "yellow", "green", "red" };

const int tempLess = 1, tempMore = 2;
const int predator = 3, noPredator = 4;
const int mate = 5,     noMate = 6;


class Chameleon
{
public:
    // default ctor ...
    Chameleon() : moodState(COLD), colourState(brown), tempState(low)
    {
        eventArr[tempLess] = &TempLess;
        eventArr[tempMore] = &TempMore;
        eventArr[predator] = &Predator;
        eventArr[noPredator] = &NoPredator;
        eventArr[mate] = &Mate;
        eventArr[noMate] = &NoMate;
    }

    void display()
    {
        cout << string( 24, '*' ) << "\n\n";
        cout << " State is " << STATES[moodState] << endl;
        cout << " Color is " << COLOURS[colourState] << endl;
        cout << " Temperature is " << TEMPS[tempState] << "\n\n";
    }
    void processEvent(int event)
    {
        (this->*eventArr[event]) ();
        display();
    }

private:
    void TempLess()
    {
        colourState = brown;
        tempState = low;
    }
    void TempMore()
    {
        colourState = green;
        tempState = high;
    }
    void Predator()
    {
        moodState = ANGRY;
        tempState= high;
        colourState = red;
    }
    void NoPredator()
    {
        moodState = WARM;
        tempState = high;
    }
    void Mate()
    {
        colourState = yellow;
        moodState = ENRAPTURED;
        tempState = high;
    }
    void NoMate()
    {
        moodState = COLD;
        tempState = low;
        colourState = yellow;
    }


    ChameleonState moodState;
    ColourState colourState;
    TempState tempState;

    void(Chameleon::*eventArr[7]) ();
};





int main()
{
    Chameleon* s = new Chameleon();

    cout << string( 24, '*' );
    cout << "\nDefault start state is:\n";
    s->display();


    int event = -1;
    while( event != 0 )
    {
        // s->display();

        cout << "Chameleon Events ... \n";
        cout << "temp < 50               1 " << endl;
        cout << "temp >= 50              2 " << endl;
        cout << "predator present        3 " << endl;
        cout << "predator not present    4 " << endl;
        cout << "mate present            5 " << endl;
        cout << "mate not present        6 " << endl;
        cout << "Enter a number 1..6 (0 to quit) > "
             << flush;

        if( cin >> event )
        {
            cout << endl;
            if( event >= 0 && event <= 6 )
            {
                cout << "You selected event " << event << " ... \n";
                if( event != 0 )
                    s->processEvent( event );
            }
            else
                cout << "'" << event << "' is NOT in valid range of (0..6)\n";

            /*

            switch( event ) // you had = //
            {
            case tempLess :
                s->processEvent(tempLess);
                break;
            case tempMore :
                s->processEvent(WARM);
                break;
            case predator :
                s->processEvent(ANGRY);
                break;
            case noPredator :
                s->processEvent(WARM);
                break;
            case mate     :
                s->processEvent(ENRAPTURED);
                break;
            case noMate   :
                s->processEvent(WARM);
                break;
            }
            */
        }
        else
        {
            cin.clear();
            cin.sync();
            cout << "\nNOT a valid entry ... Try again.\n";
            event = -1;
        }
    }

    delete s;
}

Edited 1 Year Ago by David W

This also seems to handle your problem ok ...

// Chameleon2.cpp //

#include <iostream>
#include <string>

using namespace std;

enum ChameleonState { COLD, WARM, ANGRY, ENRAPTURED };
const string STATES[] = { "COLD", "WARM", "ANGRY", "ENRAPTURE" };


enum TempState { low, high };
const string TEMPS[] = { "<50 ", ">=50 " };

enum ColourState { brown, yellow, green, red };
const string COLOURS[] = { "brown", "yellow", "green", "red" };

const int tempLess = 1, tempMore = 2;
const int predator = 3, noPredator = 4;
const int mate = 5,     noMate = 6;


class Chameleon
{
public:
    // default ctor... //
    Chameleon() : moodState(COLD), colourState(brown), tempState(low)
    {
        eventArr[tempLess] = &Chameleon::TempLess;
        eventArr[tempMore] = &Chameleon::TempMore;
        eventArr[predator] = &Chameleon::Predator;
        eventArr[noPredator] = &Chameleon::NoPredator;
        eventArr[mate] = &Chameleon::Mate;
        eventArr[noMate] = &Chameleon::NoMate;
    }

    void display()
    {
        cout << string( 24, '*' ) << "\n\n";
        cout << " State is " << STATES[moodState] << endl;
        cout << " Color is " << COLOURS[colourState] << endl;
        cout << " Temperature is " << TEMPS[tempState] << "\n\n";
    }
    void processEvent(int event)
    {
        (this->*eventArr[event]) ();
        display();
    }

private:
    void TempLess()
    {
        colourState = brown;
        tempState = low;
    }
    void TempMore()
    {
        colourState = green;
        tempState = high;
    }
    void Predator()
    {
        moodState = ANGRY;
        tempState= high;
        colourState = red;
    }
    void NoPredator()
    {
        moodState = WARM;
        tempState = high;
    }
    void Mate()
    {
        colourState = yellow;
        moodState = ENRAPTURED;
        tempState = high;
    }
    void NoMate()
    {
        moodState = COLD;
        tempState = low;
        colourState = yellow;
    }


    ChameleonState moodState;
    ColourState colourState;
    TempState tempState;


    void( Chameleon::*eventArr[7] ) ();
};





int main()
{
    cout << string( 24, '*' );
    cout << "\nDefault start state is:\n";

    Chameleon s;
    s.display();

    int event = -1;
    while( event != 0 )
    {
        cout << "Chameleon Events ... \n";
        cout << "temp < 50               1 " << endl;
        cout << "temp >= 50              2 " << endl;
        cout << "predator present        3 " << endl;
        cout << "predator not present    4 " << endl;
        cout << "mate present            5 " << endl;
        cout << "mate not present        6 " << endl;
        cout << "Enter a number 1..6 (0 to quit) > "
             << flush;

        if( cin >> event )
        {
            cout << endl;
            if( event >= 0 && event <= 6 )
            {
                cout << "You selected event " << event << " ... \n";
                if( event != 0 )
                    s.processEvent( event );
            }
            else
                cout << "'" << event << "' is NOT in valid range of (0..6)\n";
        }
        else
        {
            cin.clear();
            cin.sync();
            cout << "\nNOT a valid entry ... Try again.\n";
            event = -1;
        }
    }
}

Edited 1 Year Ago by David W

Recalling ...

... did not at all attempt to implement your 'state diagram'

The following code (that uses a typedef) is more compact and thus easier to follow:

// Chameleon3.cpp //

#include <iostream>
#include <string>

using namespace std;

enum ChameleonState { COLD, WARM, ANGRY, ENRAPTURED };
const string STATES[] = { "COLD", "WARM", "ANGRY", "ENRAPTURE" };


enum TempState { low, high };
const string TEMPS[] = { "<50 ", ">=50 " };

enum ColourState { brown, yellow, green, red };
const string COLOURS[] = { "brown", "yellow", "green", "red" };



class Chameleon
{
public:
    // default ctor... //
    Chameleon() : moodState(COLD), colourState(brown), tempState(low) {}

    void display()
    {
        cout << string( 24, '*' ) << "\n\n";
        cout << " State is " << STATES[moodState] << endl;
        cout << " Color is " << COLOURS[colourState] << endl;
        cout << " Temperature is " << TEMPS[tempState] << "\n\n";
    }
    void processEvent( int event )
    {
        (this->*fns[event]) ();
        display();
    }

private:
    void TempLess()
    {
        colourState = brown;
        tempState = low;
    }
    void TempMore()
    {
        colourState = green;
        tempState = high;
    }
    void Predator()
    {
        moodState = ANGRY;
        tempState= high;
        colourState = red;
    }
    void NoPredator()
    {
        moodState = WARM;
        tempState = high;
    }
    void Mate()
    {
        colourState = yellow;
        moodState = ENRAPTURED;
        tempState = high;
    }
    void NoMate()
    {
        moodState = COLD;
        tempState = low;
        colourState = yellow;
    }

    ChameleonState moodState;
    ColourState colourState;
    TempState tempState;

    typedef void (Chameleon::*Func) ();
    static const Func fns[6];
} ;


const Chameleon::Func Chameleon::fns[6] =
{
    &Chameleon::TempLess, &Chameleon::TempMore,
    &Chameleon::Predator, &Chameleon::NoPredator,
    &Chameleon::Mate, &Chameleon::NoMate
};





int main()
{
    cout << string( 24, '*' );
    cout << "\nDefault start state is:\n";

    Chameleon s;
    s.display();

    int event = -1;
    while( event != 0 )
    {
        cout << "Chameleon Events ... \n";
        cout << "temp < 50               1 " << endl;
        cout << "temp >= 50              2 " << endl;
        cout << "predator present        3 " << endl;
        cout << "predator not present    4 " << endl;
        cout << "mate present            5 " << endl;
        cout << "mate not present        6 " << endl;
        cout << "Enter a number 1..6 (0 to quit) > "
             << flush;

        if( cin >> event )
        {
            cout << endl;
            if( event >= 0 && event <= 6 )
            {
                cout << "You selected event " << event << " ... \n";
                if( event != 0 )
                    s.processEvent( event-1 );
            }
            else
                cout << "'" << event << "' is NOT in valid range of (0..6)\n";
        }
        else
        {
            cin.clear();
            cin.sync();
            cout << "\nNOT a valid entry ... Try again.\n";
            event = -1;
        }
    }
}

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.