如何让printf显示枚举类型变量的值?例如:
typedef enum {Linux, Apple, Windows} OS_type;
OS_type myOS = Linux;
Run Code Online (Sandbox Code Playgroud)
而我需要的是类似的东西
printenum(OS_type, "My OS is %s", myOS);
Run Code Online (Sandbox Code Playgroud)
必须显示字符串"Linux",而不是整数.
我想,首先我必须创建一个值索引的字符串数组.但我不知道这是否是最美妙的方式.有可能吗?
好的,我是C++的新手.我收到了Bjarne的书,我正在尝试遵循计算器代码.
但是,编译器正在吐出有关此部分的错误:
token_value get_token()
{
char ch;
do { // skip whitespace except '\n'
if(!std::cin.get(ch)) return curr_tok = END;
} while (ch!='\n' && isspace(ch));
switch (ch) {
case ';':
case '\n':
std::cin >> WS; // skip whitespace
return curr_tok=PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=ch;
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '.':
std::cin.putback(ch);
std::cin >> …Run Code Online (Sandbox Code Playgroud)