I have a notepad that consist of two data which is the title and the name. I've declared the title using an enum.

The enum contain this variables.

enum Title {Miss, Mrs, Mr, Dr, Unknown};

Then in my structure, I attempted to call the variables from the enum like this

struct MyInfo
{
    char name[MAX];
    Title title;
};

I'm trying to extract value from the file and display it out on the monitor. Hence, i used the ifstream method to get the information from the file

while(!afile.eof())
{
     afile.getline(info.name, MAX);
     afile.getline(info.title, MAX);
}

I do have another function to strcpy a label into the title, however, i could not extract out the value from the enum in the file via the ifstream

Recommended Answers

All 3 Replies

I would be guessing if by notepad you meant a "file." Your question reads better if I change your words to ones programmers around me use when discussing code, files and parsing.

In fact your post reads like your first quarter assignment to read and parse a file. Parsing input is a little done.

So let's get down to work. Ready? What question did you have? I see code, some statements by you but no question.

I'm seeing this for the getline spec.

istream& getline (char* s, streamsize n );

http://www.cplusplus.com/reference/istream/istream/getline/

So you are providing an argument of type Title and the function expects a char*. Seems to me you need to declare a character buffer, then write a function that takes a char* and returns a Title.

char buffer[MAX];
while(!afile.eof())
{
     afile.getline(info.name, MAX);
     info.title = toTitle(afile.getline(buffer, MAX));
}

If read what your doing, you're getting an int as the value of the enum, you can use static_cast<Title> to convert that int to an enum value. Once that is done you can use a map<Title,string>to convert that to a displayable string. I converted your struct to a class to allow more options that will give you more control over what happens to the properties. If the enum isn't required, you can use map<int,string> instead and just pass the int:

enum Title { Miss, Mrs, Mr, Dr, Unknown };
class MyInfo
{

    map<Title, string> titles;
    string name;
    Title title;
public:
    MyInfo()
    {
        if (titles.size() == 0)
        {
            titles[Miss] = "Miss";
            titles[Mrs] = "Mrs";
            titles[Mr] = "Mr";
            titles[Dr] = "Dr";
            titles[Unknown] = "Unknown";
        }

    }
    void SetTitle(Title newTitle)
    {
        title = newTitle;
    }
    void SetName(string newName)
    {
        name = newName;
    }
    string GetTitle()
    {
        return titles[title];
    }
    string GetName()
    {
        return name;
    }
};

To set the properties and display them, using the enum, is like so:

MyInfo newInfo;
newInfo.SetTitle(static_cast<Title>(3));
newInfo.SetName("Jones");
cout << newInfo.GetTitle() << newInfo.GetName() << '\n';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.