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;
}