Re: "typedef" and "enum" problem
From: Ian McCulloch (ianmcc_at_physik.rwth-aachen.de)
Date: 01/27/05
- Next message: Ian McCulloch: "Re: virtual copy constructor"
- Previous message: David White: "Re: referencing "this" in a static method"
- In reply to: Fao: ""typedef" and "enum" problem"
- Next in thread: Fao: "Re: "typedef" and "enum" problem"
- Reply: Fao: "Re: "typedef" and "enum" problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Ian McCulloch: "Re: virtual copy constructor"
- Previous message: David White: "Re: referencing "this" in a static method"
- In reply to: Fao: ""typedef" and "enum" problem"
- Next in thread: Fao: "Re: "typedef" and "enum" problem"
- Reply: Fao: "Re: "typedef" and "enum" problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|