0

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

4
Contributors
3
Replies
16
Views
10 Months
Discussion Span
Last Post by tinstaafl
0

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.

0

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));
}
0

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';

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.