S
subaruwrx88011
I am very new to c++ and very new to maps. Below is the source of my
program. The output is very strange. Am I inserting correctly into the
map? Any help will be greatly appreciated.
#include <string>
#include <iostream>
#include <map>
#include <sstream>
class CCSI_Env
{
public:
int setItemState(std::string item, std::string state);
void print();
private:
struct StateStruct
{
std::string type;
union
{
int m_integer;
char* m_string;
float m_float;
} stateUnion;
};
//Map with string for the key and StateType for Value
typedef std::map<std::string, StateStruct> ItemStateMap;
//Item/State value pair of the enviroment
static ItemStateMap m_itemMap;
};
CCSI_Env::ItemStateMap CCSI_Env::m_itemMap = ItemStateMap();
int CCSI_Env::setItemState(std::string p_item, std::string p_state)
{
StateStruct itemState;
//Store state as union
itemState.type = "STRING";
itemState.stateUnion.m_string = (char*)p_state.c_str();//Convert to
char*
//Store item with state in map
m_itemMap[p_item] = itemState;
return 0;
}
void CCSI_Env:rint()
{
ItemStateMap::iterator mapIterator;
std::string message;
std::cout<<"******"<<std::endl;
for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
if(mapIterator->second.type == "STRING")
{
message = mapIterator->first + " = " +
mapIterator->second.stateUnion.m_string;
std::cout << message << std::endl;
}
}
std::cout<<"******"<<std::endl;
}
int main()
{
CCSI_Env m_env;
int status;
std::string s,t;
s = "ONE";
t = "Online";
status = m_env.setItemState(s,t);
m_env.print();
s = "TWO";
t = "Online";
status = m_env.setItemState(s,t);
m_env.print();
s = "THREE";
t = "Offline";
status = m_env.setItemState(s,t);
m_env.print();
s = "FOUR";
t = "Offline";
status = m_env.setItemState(s,t);
m_env.print();
}
The output should be:
******
ONE = Online
******
******
ONE = Online
TWO = Online
******
******
ONE = Online
THREE = Offline
TWO = Online
******
******
FOUR = Offline
ONE = Online
THREE = Offline
TWO = Online
******
program. The output is very strange. Am I inserting correctly into the
map? Any help will be greatly appreciated.
#include <string>
#include <iostream>
#include <map>
#include <sstream>
class CCSI_Env
{
public:
int setItemState(std::string item, std::string state);
void print();
private:
struct StateStruct
{
std::string type;
union
{
int m_integer;
char* m_string;
float m_float;
} stateUnion;
};
//Map with string for the key and StateType for Value
typedef std::map<std::string, StateStruct> ItemStateMap;
//Item/State value pair of the enviroment
static ItemStateMap m_itemMap;
};
CCSI_Env::ItemStateMap CCSI_Env::m_itemMap = ItemStateMap();
int CCSI_Env::setItemState(std::string p_item, std::string p_state)
{
StateStruct itemState;
//Store state as union
itemState.type = "STRING";
itemState.stateUnion.m_string = (char*)p_state.c_str();//Convert to
char*
//Store item with state in map
m_itemMap[p_item] = itemState;
return 0;
}
void CCSI_Env:rint()
{
ItemStateMap::iterator mapIterator;
std::string message;
std::cout<<"******"<<std::endl;
for(mapIterator = m_itemMap.begin(); mapIterator != m_itemMap.end();
mapIterator++)
{
if(mapIterator->second.type == "STRING")
{
message = mapIterator->first + " = " +
mapIterator->second.stateUnion.m_string;
std::cout << message << std::endl;
}
}
std::cout<<"******"<<std::endl;
}
int main()
{
CCSI_Env m_env;
int status;
std::string s,t;
s = "ONE";
t = "Online";
status = m_env.setItemState(s,t);
m_env.print();
s = "TWO";
t = "Online";
status = m_env.setItemState(s,t);
m_env.print();
s = "THREE";
t = "Offline";
status = m_env.setItemState(s,t);
m_env.print();
s = "FOUR";
t = "Offline";
status = m_env.setItemState(s,t);
m_env.print();
}
The output should be:
******
ONE = Online
******
******
ONE = Online
TWO = Online
******
******
ONE = Online
THREE = Offline
TWO = Online
******
******
FOUR = Offline
ONE = Online
THREE = Offline
TWO = Online
******