"typedef" and "enum" problem

Discussion in 'C++' started by Fao, Jan 27, 2005.

  1. Fao

    Fao Guest

    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

    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.

    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};
    void PrintEnum();

    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;
    else
    Color = red;
    break;

    case 'b': Color = blue;
    break;

    case 'c': if (ch2 == 'w')
    Color = white;
    else
    Color = yellow;
    break;

    case 'd': Color = purple
    break;
    default: cout << "Illegal input." << endl;

    return Colors;
    }

    void PrintEnum(Favorite Colors)
    {
    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;
    }
    }

    If anyone, and I mean anyone out there can help me, it would be greatly
    appreciated.
     
    Fao, Jan 27, 2005
    #1
    1. Advertising

  2. Fao

    gooch Guest

    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
    >
    > 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.
    >
    > 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};
    > void PrintEnum();
    >
    > int main();
    > {
    > cout << "Enter the First two letters of your favorite color: " <<

    endl;
    >
    > cin >> ch1 >> ch2;


    What are ch1 and ch2 you never declared them
    >
    > switch (ch1)
    > {
    > case 'a': if (ch2 == '1')
    > Color = black;
    > else
    > Color = red;
    > break;
    >
    > case 'b': Color = blue;
    > break;
    >
    > case 'c': if (ch2 == 'w')
    > Color = white;
    > else
    > Color = yellow;
    > break;
    >
    > case 'd': Color = purple
    > break;
    > default: cout << "Illegal input." << endl;
    >
    > return Colors;


    again what is Colors you did not declare this.
    > }


    how do these values map to the colors. You aked for the first two
    letters of your favorite color and as far as I can see that is not what
    you are looking for.
    >
    > void PrintEnum(Favorite Colors)

    What is type Favorite
    > {
    > 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;
    > }
    > }
    >
    > If anyone, and I mean anyone out there can help me, it would be

    greatly
    > appreciated.


    I am not going to do the assignment for you and I doubt anyone else
    here is either. I think that you would find, judging by what you have
    above, that if someone here did the assignment your teacher would
    surely know that you did not do it.

    My first suggestion would be read your textbook from the beginning and
    try again. Any, even half ass, book on C++ will cover these topics very
    early in the book. You should also go and talk to your teacher and TA
    if you have one. Go to a study group. Look at sample programs in the
    book and try to see what they are doing.

    You are missing some very basic things here and I don't have time to
    write you a tutorial here. There are a lot of tutorials on the web, try
    http://www.cplusplus.com/doc/tutorial/ or
    http://www.intap.net/~drw/cpp/. I just did a quick google search for
    C++ tutorials and got over 4 million hits.

    Once you take a look at the materials and make a real effort to get
    together something that reasonably resembles a C++ program I would be
    glad to give you some pointers.
     
    gooch, Jan 27, 2005
    #2
    1. Advertising

  3. 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::eek:stream& operator<<(std::eek:stream& 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::eek:stream& operator<<(std::eek:stream& 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
     
    Ian McCulloch, Jan 27, 2005
    #3
  4. Fao

    Fao Guest

    I thank you for you reply, and I really did not want anyone to do the
    assignment for me, it's just that I am lost. This is only the third
    week of school and my professor has me doing "typedef" and "enum"
    functions in chapter 8 of our book, and I am seriously new to
    programming (no prior experience what so ever). My book gives examples
    but they have nothing to do with the type of program he wants the class
    to complete. With that said, I will take your advice and check out the
    tutorial sites you mentioned, considering I have been bugging my
    teacher for the past two days about the assignment and he refuses to
    help.
    And just so you do not think I am a total moron, here is a program I
    had to complete before this assignment (It might be simple compared to
    the kind of stuff you do, but for someone who have never done this
    stuff until this semester, I think I understand it better than most of
    the people in my class.) Thanks again! :

    #include <iostream>
    #include <cmath>
    using namespace std;

    double number;
    const int SENTINEL = -999;
    void rootofNumber(double x);

    int main()
    {

    double num, num2;
    double sum = 0;
    double max ;
    int counter = 2;
    int avg = 0;
    double secMax;

    cout << "Enter numbers : To stop program enter" << " " << SENTINEL <<
    endl;

    rootofNumber(number);

    cin >> num2;

    if (number > num2)
    {
    max = number;
    secMax = num2;
    }

    else
    {
    max = num2;
    secMax = number;
    }


    sum = sum + number + num2;
    cin >> num;

    while (num != SENTINEL)
    {

    sum = sum + num;

    if (num > max)
    {
    secMax = max;
    max = num;

    }


    if (max > num && num > secMax)
    {
    secMax = num;
    }

    counter++;

    avg = sum / counter;

    cin >> num;

    }


    cout << "Sum of numbers is: " << sum << endl;
    cout << "The avg is: " << avg << endl;
    cout << "The max number is: " << max << endl;
    cout << "The Second max is: " << secMax << endl;


    return 0;
    }

    void rootofNumber(double x)
    {

    cin >> number;

    cout << "The Squareroot of "<< number << " is "<< sqrt(number)
    << endl;
    return;
    }
     
    Fao, Jan 27, 2005
    #4
  5. Fao

    Fao Guest

    Thanks for your reply. The scenario you presented could solve my
    problem, except I cannot use strings, arrays, or structs. If I could, I
    would probably be done. When you stated:

    "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?"

    You make the same argument I have been making to my Professor for the
    past 2 days!!!
    But he says there is a way to do it, I just have to find it.

    The instructions that I posted were the exact ones he gave to letter.
    Thanks again for your reply, I will research everything you submitted.
     
    Fao, Jan 27, 2005
    #5
  6. Fao

    Guest

    Fao wrote:
    > But he says there is a way to do it, I just have to find it.

    HINT: What other type has only two values? Has this type always been a
    part of C++? If not, how would you get by without it?
     
    , Jan 27, 2005
    #6
  7. Fao

    gooch Guest

    Fao wrote:
    > I thank you for you reply, and I really did not want anyone to do the
    > assignment for me, it's just that I am lost. This is only the third
    > week of school and my professor has me doing "typedef" and "enum"
    > functions in chapter 8 of our book, and I am seriously new to
    > programming (no prior experience what so ever). My book gives

    examples
    > but they have nothing to do with the type of program he wants the

    class
    > to complete. With that said, I will take your advice and check out

    the
    > tutorial sites you mentioned, considering I have been bugging my
    > teacher for the past two days about the assignment and he refuses to
    > help.

    Sounds like you might not have a very good teacher.

    > And just so you do not think I am a total moron, here is a program I
    > had to complete before this assignment (It might be simple compared

    to
    > the kind of stuff you do, but for someone who have never done this
    > stuff until this semester, I think I understand it better than most

    of
    > the people in my class.) Thanks again! :


    I am not trying to imply you are a "moron" just that there are some
    very basic elements, i.e. variable declarations, missing from your
    code. This program is a whole different story from the one you posted
    the first time. I would suggest you use the first program you did as a
    starting point for the new one. Change one thing at a time and
    recompile and run it so you can make sure that the one thing works. If
    it won't compile you will have a very good idea of where the problem
    is. When you get something together and still have some questions post
    them again.
     
    gooch, Jan 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp

    typedef enum

    qazmlp, Jul 2, 2003, in forum: C++
    Replies:
    2
    Views:
    479
    Alexander Terekhov
    Jul 2, 2003
  2. Chris
    Replies:
    4
    Views:
    15,800
    Ben Pfaff
    May 15, 2004
  3. Martin

    typedef declares object / enum to int

    Martin, Feb 23, 2005, in forum: C Programming
    Replies:
    13
    Views:
    1,739
    Richard Bos
    Feb 24, 2005
  4. oor
    Replies:
    0
    Views:
    1,416
  5. Brian
    Replies:
    4
    Views:
    2,782
    Brian
    Feb 27, 2010
Loading...

Share This Page