Enum and struct question

So I got this line in a txt file:

Libra 10 - 10 - 1980

with this declaration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum Zodiac 
{
	Aquarius, Pisces, Aries, Taurus, 
	Gemini, Cancer, Leo, Virgo, 
	Libra, Scorpio, Sagittarius, Capricorn 
}; 

struct Date 
{
	Zodiac sign; 
	int day; 
	int month; 
 	int year; 
};
 
struct Info
{ 
 	char name [MAX]; 
 	char nationality [MAX]; 
	Date birthDate; 
};


so now I want to read the "Libra" from the txt file and initialize it in the Info struct (which need to be initialized in the Date struct first). As far as I know, the "Libra" value in the enum Zodiac will have a value of 8.

So my question is how do I even read the "libra" in the text file to initialize it in the Date struct?

upon more researching, would "readfile >> Info.birthdate.sign;" works?
Last edited on
You're on the right track.

You would have to read in the whole line (see http://www.cplusplus.com/doc/tutorial/files/), and then parse than line into separate variables (e.g. a string for the zodiac name, ints for the day month and year).

Then create your Info object.
 
Info myInformation;


Then assign your variables like this:

 
myInformation.birthdate.year = year;  // where year on the rhs is your parsed int 


Is this sposed to be C or C++? if you can use C++ i would use std::string's for your name and nationality.
Last edited on
You're going to have to make a conversion from a text string into an enum.

Something like this:
1
2
3
4
5
6
7
8
9
  string temp;
  Info info;

  readfile >> temp;
  if (bad())
    // deal with error 
  if (temp == "Aquarius") 
    info.birthDate.sign = Aquarius;  // Assign the enum value
  ... // Repeat for each of the remaining signs  


Last edited on
or change your enum to a collection and iterate over this to at least make sure that whatever the user has entered as a zodiac name is valid.
Thanks for all the response guys, I think i can use Abstration's algorithm.

Forgot to mention that I'm using C++ (IDE is Quincy 2005)

@mutexe: I don't think I'm able to use collection yet. As for the nationality, it's by itself on a separate line so I'm just going to use

 
 readfile.getline (info.nationality, MAX);


Another thing, if I want to output the zodiac on the monitor, can I use a similar algorithm?

1
2
3
4
5
6
7
int temp;
temp = info.birthDate.Zodiac;
switch (temp)
{
 case 0: cout << "Aquarius" <<endl;
 ... // Rise and repeat
}
Last edited on
I don't think I'm able to use collection yet


What do you think these are:

1
2
char name [MAX]; 
char nationality [MAX]; 

?

:)
umm, aren't they strings?

Sorry I'm really new to all these
Last edited on
strings yes, but represented by a collection of characters.
Anyway, stick with AbstractionAnon's suggestion :)
so by "collection" you meant an array?
yep sorry. A collection is just a bunch of stuff, wther it be a STL vector, List, or C-style array.
Topic archived. No new replies allowed.