Re: "typedef" and "enum" problem

From: Ian McCulloch (ianmcc_at_physik.rwth-aachen.de)
Date: 01/27/05


Date: Thu, 27 Jan 2005 03:28:20 +0100

Fao wrote:

> Hi, I am in my first year of C++ in college and my professor wants me
> to Write a Program with multiple functions,to input two sets of
> user-defined data types:
>
> One type named 'Sign' declared by "typedef" to contain only
> either +10 or -10 and

That doesn't make sense, you can't define, using a typedef, a type that can
contain 2 values, +10 or -10. What exactly are you required to do?

>
> the other type named Color declared by "enum" to contain only
> black, blue, purple, red, white, and yellow.
>
> For each set of inputs, output their sum, average,
> maximum and second largest.

What exactly are the inputs? that doesn't make sense with respect to either
of the input types you have described.

>
> I must also use typedef and enum for two data types.
>
>
> I do not have a problem with finding the sum, max, avg, second largest,
> my problem is how to properly use "typedef" and "enum".
>
> Here is what I have, which is completely wrong(laugh if you want):
>
> #include <iostream>
> using namespace std;
>
> enum Color {black, blue, purple, red, white, yellow};

That enum is fine.

> void PrintEnum();

A function that takes no parameters and returns no value. What is it
supposed to do?

Did you want

std::ostream& operator<<(std::ostream& out, Color c);

instead?

>
> int main();
> {
> cout << "Enter the First two letters of your favorite color: " << endl;
>
> cin >> ch1 >> ch2;
>
> switch (ch1)
> {
> case 'a': if (ch2 == '1')
> Color = black;

That doesn't make sense. Why should the sequence "a1" correspond to black?

> else
> Color = red;
> break;
>
> case 'b': Color = blue;
> break;
>
> case 'c': if (ch2 == 'w')
> Color = white;

And why should "cw" be white?

> else
> Color = yellow;
> break;
>
> case 'd': Color = purple

"d" -> purple???

> break;
> default: cout << "Illegal input." << endl;
>
> return Colors;

The only 2 return values from main() defined by the C++ standard are
EXIT_SUCCESS and EXIT_FAILURE (I think - and I can't remember what header
they are defined in at the moment). As a special exception, if there is no
return statement, it is equivalent to "return EXIT_SUCCESS". Returning
'Colors' is meaningless.

> }
>
> void PrintEnum(Favorite Colors)

The signature must match the prototype you declared previously. It should
also mention the types that you want. eg, PrintEnum(Color Colors), meaning
take 1 argument, named 'Colors' of type 'Color'. The overload of
operator<< is a better solution though.

> {
> switch (Colors)
> {
> case black: cout << "black";
> break;
> case blue: cout << "blue";
> break;
> case purple: cout << "purple";
> break;
> case red: cout << "red";
> break;
> case white: cout << "white";
> break;
> case yellow: cout << "yellow";
> break;

Ok, but a better approach would be

std::ostream& operator<<(std::ostream& out, Color c)
{
   return out << ColorStrings[c];
}

where ColorStrings is

char const* ColorStrings[6] =
{"black","blue","purple","red","white","yellow"};

You could re-use this array to improve the input of the colors (ie. by
finding the index of the string that matches the first 2 characters of the
input).

> }
> }
>
> If anyone, and I mean anyone out there can help me, it would be greatly
> appreciated.

HTH,
Ian McCulloch



Relevant Pages

  • Mark old list-based power managment as deprecated
    ... * Power management requests ... +typedef enum pm_dev_type ...
    (Linux-Kernel)
  • Re: typedef question
    ... It's inadvisable to use identifiers starting with underscores. ... If you insist on using a typedef, you can use the same identifier for ... the typedef and the enum tag -- or you can drop the enum tag ... Keep in mind that the enumeration constants enum1 et al are not of ...
    (comp.lang.c)
  • Re: Totally broken PCI PM calls
    ... > enum, btw. ... You can just do a regular integer typedef and mark the typedef ... +typedef enum pm_dev_type __bitwise { ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • "typedef" and "enum" problem
    ... black, blue, purple, red, white, and yellow. ... I must also use typedef and enum for two data types. ...
    (comp.lang.cpp)
  • C# Interop of C dll passing a struct
    ... typedef unsigned char PK_U8,*PK_PU8; 8-bit unsigned char type. ... typedef PK_WORD TSwitchIndex; Switch index type. ... typedef PK_WORD TSwitchStream, *PSwitchStream; Switch stream type. ... public static extern int CASGroupInfo(int nBoard, ref TCASGroupInfo groupInfo); ...
    (microsoft.public.dotnet.framework.interop)