Home > OS >  How to properly print enum type
How to properly print enum type

Time:03-31

I have this class code in c :

#include <iostream>
#include <cstring>
using namespace std;
enum tip{txt, 
pdf, 
exe
};
class File{
    private:
    char *imeDatoteka{nullptr};
    tip t;
    char *imeSopstvenik{nullptr};
    int goleminaFile = 0;
public:
    File(){}
    File(char *i, char *imeS, int golemina, tip tip){
        t = tip;
        goleminaFile = golemina;
        imeDatoteka = new char[strlen(i) 1];
        imeSopstvenik = new char[strlen(imeS) 1];
        strcpy(imeDatoteka, i);
        strcpy(imeSopstvenik, imeS);
    }
    File(const File &f){
        t = f.t;
        goleminaFile = f.goleminaFile;
        imeDatoteka = new char[strlen(f.imeDatoteka) 1];
        imeSopstvenik = new char[strlen(f.imeSopstvenik) 1];
        strcpy(imeDatoteka, f.imeDatoteka);
        strcpy(imeSopstvenik, f.imeSopstvenik);
    }
    ~File(){
        delete [] imeDatoteka;
        delete [] imeSopstvenik;
    }
    File &operator=(const File &f){
        if(this != &f){
            t = f.t;
            goleminaFile = f.goleminaFile;
            delete [] imeDatoteka;
            delete [] imeSopstvenik;
            imeDatoteka = new char[strlen(f.imeDatoteka) 1];
            imeSopstvenik = new char[strlen(f.imeSopstvenik) 1];
            strcpy(imeDatoteka, f.imeDatoteka);
            strcpy(imeSopstvenik, f.imeSopstvenik);
        }
        return *this;
    }
    void print(){
        cout<<"File name: "<<imeDatoteka<<"."<<(tip) t<<endl;
        cout<<"File owner: "<<imeSopstvenik<<endl;
        cout<<"File size: "<<goleminaFile<<endl;
    }
    bool equals(const File & that){
        if((strcmp(imeDatoteka, that.imeDatoteka) == 0)  && (t == that.t) && (strcmp(imeSopstvenik, that.imeSopstvenik) == 0))
            return true;
            else
                return false;
    }
    bool equalsType(const File & that){
        if((strcmp(imeDatoteka, that.imeDatoteka) == 0) && (t == that.t))
            return true;
            else
                return false;
    }
};

And i have a problem. So i have an private member 'tip' that is enum type. The problem is it doesn't print it correctly(pdf,txt or exe), it just prints 0,1 or 2. I've seen some people try to cast it in the cout but it doesn't work for me. Any help?

CodePudding user response:

You could create a lookup table using map and string:

#include <map>
#include <string>

std::map<tip, std::string> tip_to_string = {
    {txt, "txt"},
    {pdf, "pdf"},
    {exe, "exe"}
};

And then when you want to print some tip t:

std::cout << tip_to_string.at(t) << std::endl;

Or you could do a function:

std::string tip_to_string(tip t) {
    switch(t) {
        case txt:
           return "txt";
        case pdf:
           return "pdf";
        case exe:
           return "exe";
        default:
           return "You forgot to add this tip to the tip_to_string function.";
    }
}

And then when you want to print some tip t:

std::cout << tip_to_string(t) << std::endl;

I don't think there's a way to just print an enum as a string, but somebody who knows more about C could probably answer that. This might be a helpful read: https://en.cppreference.com/w/cpp/language/enum

CodePudding user response:

#include <vector>
#include <string>

enum class Color
{
    RED,
    BLUE
};

constexpr std::vector<Color>          COLORS_ENUMS = { Color::RED, Color::BLUE };
constexpr std::vector<std::string>    COLORS_STRGS = { [Color::RED] = "RED", [Color::BLUE] = "BLUE" };

C 20 should support this, and then you can do something like:

const Color r = Color::RED;
const std::string& str_of_enum = COLORS_STRGS[r];

This hasn't been tested, so please correct me if I am wrong.

  • Related