You can't assign arrays. You can either change the type of value
to a char*
or copy the content of generalOptions[0]
into value
. If you are going to copy the content, then you need to ensure that value
has enough space to hold the content of the element in generalOperations
.
Modifying a string literal is undefined behaviour, by changing the type to const char*
the compiler can detect any attempt to modify one of the entries in generalOperations
instead of experiencing odd behaviour at runtime:
const char* generalOperations [2]={"Off","On"};
const char* value;
Note you don't have to specify the number of elements in the array if you are initialising it:
const char* generalOperations [] = {"Off","On"};
Or, if this really is C++ you can make value
a std::string
instead and just assign to it which will copy the generalOperations
element.
As C++ appears to really be the language and C++11 features are permitted instead of using a switch
you could create a std::map
that associates the int
values with the std::string
:
#include <iostream>
#include <string>
#include <map>
const std::map<int, std::string> generalOperations{ {17, "Off"},
{29, "On" } };
int main()
{
auto e = generalOperations.find(17);
if (e != generalOperations.end())
{
// Do something with e->second.
std::cout << e->second << "\n";
}
return 0;
}
Demo: http://ideone.com/rvFxH.