C++ Program using structures to create a basic ID

I just now started learning structures and I have my program working, but how can I improve it so it incorporates eye color (using enumerated types), and full street address.
For example the user will enter 123 Oak Street San Antonio TX and it returns the address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct date
{
	unsigned int month, day, year;
};

struct fullname
{
	string firstname, lastname;
	char initial;
};

struct address
{
	string street, city, state;
};

enum color {BROWN, BLUE, GREEN, HAZEL}; // eye color
struct license
{
	fullname name;
	date DOB;
	address home_address;
	date expiredate;
	unsigned int weight;
	string height;
	color eyecolor;
};
void enter_data(license &driver);
void print_record(license driver);
int main(void) 
{
	license driver; 
    enter_data(driver);
    print_record(driver);
    
	return 0; 
}


void enter_data(license &driver)
{
    cout << "Enter first and last name: " << endl;
	cin >> driver.name.firstname >> driver.name.lastname;
    cout << endl;
	cout << "Enter Date of Birth as MM DD YYYY" << endl;
	cin >> driver.DOB.month >> driver.DOB.day >> driver.DOB.year;
    cout << endl;
	cout << "Enter Address: ";
	cin >> driver.home_address.street >> driver.home_address.city >> driver.home_address.state;
	cout << endl;
	cout << "Expiration Date: ";
	cin >> driver.expiredate.month >> driver.expiredate.day >> driver.expiredate.year;
	cout << endl;
	cout << "Enter weight ";
	cin >> driver.weight;
	cout << endl;
    cout << "Enter height: ";
    cin >> driver.height;
    

    return; // Nothing returned in call by reference
}

void print_record(license driver) 
{ 
	cout << driver.name.firstname << " " << driver.name.lastname << endl;
    cout << "Birthdate: " << driver.DOB.month << "/" 
         << driver.DOB.day << "/"
         << driver.DOB.year << endl;
	cout << "Address " << driver.home_address.street 
         << " " << driver.home_address.city
         << " " << driver.home_address.state << endl;
	cout << "Expiration Date: " << driver.expiredate.month
         << "/" << driver.expiredate.day 
         << "/"<< driver.expiredate.year << endl;
	cout << "Weight: " << driver.weight<< endl;
	cout << "height: " << driver.height << endl;
	return;
}


My output:

Enter first and last name: 
random name

Enter Date of Birth as MM DD YYYY
08 21 1999

Enter Address: redwood sacramento CA

Expiration Date: 07 12 2023

Enter weight 130

Enter height: 5'10
random name
Birthdate: 8/21/1999
Address redwood sacramento CA
Expiration Date: 7/12/2023
Weight: 130
height: 5'10

Last edited on
Enum <---> string is a pain. But if you must, you need functions to convert between the two, and you must be able to accept or reject bad input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
enum EyeColor
{
  EYE_BLUE,
  EYE_GREEN,
  EYE_BROWN,
  EYE_BLACK,
  ...
};

const std::string EyeColorNames[] =
{
  "blue",
  "green",
  "brown",
  "black",
  ...
};

std::ostream& operator << ( std::ostream& outs, const EyeColor& color )
{
  return outs << EyeColorNames[color];
}

std::istream& operator >> ( std::istream& ins, EyeColor& color )
{
  std::string s;
  if (ins >> s)
  {
    using std::begin;
    using std::end;
    auto iter = std::find( begin(EyeColorNames), end(EyeColorNames ), s );
    if (iter == end(EyeColorNames))
    {
      ins.setstate( std::ios::failbit );
      return ins;
    }
    color = (EyeColor)std::distance( begin(EyeColorNames), iter );
  }
  return ins;
}

Obnoxious, isn’t it?
This code is actually kind of fragile, too. It fails the ODR. And there are waaaay more eye colors than people tend to think. Enums stink.

If you can, just use a string.

Hope this hleps.
Topic archived. No new replies allowed.