In need of some help

Thanks for help!
Last edited on
> if I am using the switch statement correctly

Yes. Consider using a consistent brace placement style. https://en.wikipedia.org/wiki/Brace_style


> what should I be writing in my int main?

Something along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{ 
    bird birdType = PARROT ; // Declare a variable bird of the type birdType 

    bird = CANARY ; //  Assign CANARY to the variable bird.

    bird = birdType(bird+1) ; // Advance bird to the next value in the list.

    std::cout << bird << ". " << birdTypeToString(bird) << '\n' ; // Output the value of the variable bird. 

    // Input a value (from the user) in the variable bird.
    // TODO: display a list of birds to the user  0. PEACOCK  1. SPARROW etc.
    // TODO: accept a number from the user, set the value of bird to the corresponding enum value 
}
BTW. You could replace your switch statement with a lookup table. Long switch statements make the code more difficult to read and more difficult to maintain.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string birdTypeToString (birdType bird)
{
  const int NUM_BIRDS = 8;
  const string names[NUM_BIRDS] =
  {
    "PEACOK", "SPARROW", "CANARY", "PARROT", 
    "PENGUIN", "OSTRICH", "CARDINAL", "HUMMINGBIRD" 
  };

  int birdIdx = int (bird);
  if (birdIdx >= 0 && birdIdx < NUM_BIRDS)
    return names[birdIdx];

  return "Unknown bird";
}

How would I go about writing problem F? I can't figure out how I can I can have the user input a number and output the corresponding enum value.

Would it be an if statement? Where I have the user input a number within the if statement? Or would that not work or interfere with my switch statement?

*(Sorry if these seem like noob questions, still trying to wrap my head around this stuff)
*Thank you both for your replies! You two have been a great help. :)
bump
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
#include <iostream>
#include <string>

// each enumerator is associated with a value of the underlying type (int in this case).
// when an initialiser is present, the values of enumerator is that of the initialisers.
// if the first enumerator does not have an initializer, the associated value is zero.
// for any other enumerator which does not have an initializer, the value is one greater than the value of the previous enumerator
// here, both BIRD_MIN and PEACOCK have a value of zero, SPARROW has a value of 1, both BIRD_MAX and HUMMINGBIRD have a value of 8 (CARDINAL+1)
enum bird_type { BIRD_MIN = 0, PEACOCK = BIRD_MIN, SPARROW, CANARY, PARROT, PENGUIN, OSTRICH, EAGLE, CARDINAL, BIRD_MAX = CARDINAL+1, HUMMINGBIRD = BIRD_MAX };
const std::string bird_name[] = { "PEACOCK", "SPARROW", "CANARY", "PARROT", "PENGUIN", "OSTRICH", "EAGLE", "CARDINAL", "HUMMINGBIRD" };

std::string bird_type_to_string( bird_type bird )
{
    // values of type bird_type are implicitly-convertible to values of type int
    if( bird >= BIRD_MIN && bird <= BIRD_MAX ) return bird_name[bird] ;
    else return "UNKNOWN" ;
}

void display_bird_list() // display a list of birds
{
    for( int i = BIRD_MIN ; i <= BIRD_MAX ; ++i )
    {
        // there is no implicit conversion from an int to bird_type, a cast is required
        // the value of the int to be so converted should be within the range of the enumeration
        std::cout << i << ". " << bird_name[ bird_type(i) ] << '\n' ;
    }
}

int main()
{
    bird_type bird = PARROT ; // Declare a variable bird of the type birdType

    display_bird_list() ; // display a list of bird types

    std::cout << "\nenter a number [" << BIRD_MIN << "..." << BIRD_MAX << "] for the bird type: " ;
    int number = BIRD_MIN ;
    std::cin >> number ; // accept a number corresponding to the bird type

    if( number >= BIRD_MIN && number <= BIRD_MAX ) // if the number is within the range of the enumeration
    {
        bird = bird_type(number) ; // convert it to bird_type by with a cast
        std::cout << "bird type is " << bird_type_to_string(bird) << '\n' ;
    }
    else std::cout << number << " does not correspond to a bird type\n" ;
}
Last edited on
I am making shogi game(Japnese chess) in C++. Need some help in promotion of pieces and checkmate?
Start a new topic (Forum - General C++ Programming, click on the 'New topic' button on the top right.

Ask specific questions: what is the precise difficulty that you are facing in promotion of pieces?
Give a link or briefly explain the relevant rules of shogi. Provide a code snippet which illustrates the problem.
Topic archived. No new replies allowed.