G12 Answer


#include <stdio.h>

/* declare the enums */
typedef enum  {hot, cold} cupStyle;
typedef enum  {ml, oz} units;

/* declare the Cup type */
typedef struct
{
  cupStyle style;
  units measure;
  int capacity;
} Cup;

void printCup( Cup *cup )
{
  char *stylestring, *unitstring;
  if ( cup->style == hot ) stylestring = "hot cup";
  else stylestring = "cold cup";

  if ( cup->measure == ml ) unitstring = "ml";
  else unitstring = "oz";

  printf("%3d %s %s\n", cup->capacity, unitstring, stylestring );
}

int main(int argc, char *argv[])
{
  /* declare and initialize two Cups */
  Cup cupA = {hot, oz, 12}, cupB = {cold, ml, 16};

  /* print values of both Bulbs */
  printCup( &cupA );
  printCup( &cupB );

  system("pause");
  return 0;
}

Comments: For small structs such as Cup passing the entire struct to printCup would work as well as passing the address (as is done here).

The print function contains some tricks. Since an enum is implemented as an integer, printing it directly does not produce a nice label.

An older way of doing the style and units is to use preprocessor constants:


#define cupStyle int
#define hot 0
#define cold 1

#define units int
#define ml 0
#define oz 1

... rest of the program the same ...

This is not as elegant, nor as safe, as using enum.