Define an enum type, birdType, with the values PEACOCK, SPARROW, CANARY, PARROT,
ID: 3703772 • Letter: D
Question
Define an enum type, birdType, with the values PEACOCK, SPARROW,
CANARY, PARROT, PENGUIN, OSTRICH, EAGLE, CARDINAL, and
HUMMINGBIRD.
Write a C++ function that can be used to input value in a variable of type birdType.
Write a C++ function that can be used to ouput the value of a variable of type birdType.
Write a main program and use the functions to display an output shown below:
Enter name of the bird (PEACOCK, SPARROW, CANARY, PARROT, PENGUIN, OSTRICH, EAGLE, CARDINAL, HUMMINGBIRD) OSTRICH The bird is Ostrich ould you like to try again (y/n) Y Enter name of the bird ( PEACOCK, CANARY PARROT, OSTRICH, EAGLE, CARDINAL, HUMMINGBIRD ): SPARROW, CANARY, PENGUIN, The bird is Canary ould you like to try again (y/n)Y Enter name of the bird PEACOCK, SPARROW, CANARY, PARROT,PENGUIN,OSTRICH, EAGLE, CARDINAL, HUMMINGBIRD): Eagle ou entered invalid bird name... Press any key to continueExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
enum birdType { PEACOCK = 1, SPARROW, CANARY, PARROT, PENGUIN, OSTRICH, EAGLE, CARDINAL, HUMMINGBIRD};
void input(birdType &b);
void output(birdType b);
int main()
{
string ans;
do
{
birdType b;
cout << "Enter name of the bird" << endl;
cout << "(PEACOCK, SPARROW, CANARY, PARROT, PENGUIN, OSTRICH, EAGLE, CARDINAL, HUMMINGBIRD)" << endl;
input(b);
output(b);
cout << "Would you like to try again y/n? ";
cin >> ans;
cout << endl;
}while(ans == "y" || ans == "Y");
}
void input(birdType &b)
{
string s;
cin >> s;
if(s == "PEACOCK")
b = PEACOCK;
else if(s == "SPARROW")
b = SPARROW;
else if(s == "CANARY")
b = CANARY;
else if(s == "PARROT")
b = PARROT;
else if(s == "PENGUIN")
b = PENGUIN;
else if(s == "OSTRICH")
b = OSTRICH;
else if(s == "EAGLE")
b = EAGLE;
else if(s == "CARDINAL")
b = CARDINAL;
else if(s == "HUMMINGBIRD")
b = HUMMINGBIRD;
else
b = (birdType)0;
}
void output(birdType b)
{
switch(b)
{
case PEACOCK:
cout << "The bird is Peacock" << endl;
break;
case SPARROW:
cout << "The bird is Sparrow" << endl;
break;
case CANARY:
cout << "The bird is Canary" << endl;
break;
case PARROT:
cout << "The bird is Parrot" << endl;
break;
case PENGUIN:
cout << "The bird is Penguin" << endl;
break;
case OSTRICH:
cout << "The bird is Ostrich" << endl;
break;
case EAGLE:
cout << "The bird is Eagle" << endl;
break;
case CARDINAL:
cout << "The bird is Cardinal" << endl;
break;
case HUMMINGBIRD:
cout << "The bird is HummingBird" << endl;
break;
default:
cout << "You entered invalid bird name" << endl;
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.