172k views
5 votes
Create vector of Color variables

Use a loop to ask the user to enter the names of colors.
For each color entered, add the corresponding Color enumeration value to the vector.
Ignore any invalid color that is entered (that is, a color that doesn't exist in the enumeration).
Exit the loop when end is entered.
After exiting the loop, iterate through the vector printing out each of the values in the vector.
Sample output (user input is in bold):

What color do you want to add?
red
What color do you want to add?
yellow
What color do you want to add?
superdupergreen
What color do you want to add?
orange
What color do you want to add?
blue
What color do you want to add?
blue
What color do you want to add?
green
What color do you want to add?
indigo
What color do you want to add?
violet
What color do you want to add?
end
0
2
1
4
4
3
5
6
file included: balloon.h

balloon.h contents:

#ifndef BALLOON_H
#define BALLOON_H
enum Color {red, orange, yellow, green, blue, indigo, violet};
#endif

User Lukik
by
3.3k points

1 Answer

0 votes

Answer:

See explaination

Step-by-step explanation:

balloon.h

#ifndef BALLOON_H

#define BALLOON_H

enum Color {red, orange, yellow, green, blue, indigo, violet};

#endif

main.cpp

#include <iostream>

#include <string>

#include <vector>

#include "balloon.h"

using namespace std;

int main( )

{

vector<Color> colors;

Color color;

string input;

while(true){

cout<<"What color do you want to add? ";

cin>>input;

if(input=="end"){

break;

}else if(input=="red"){

color = red;

colors.push_back(color);

}else if(input=="orange"){

color = orange;

colors.push_back(color);

}else if(input=="yellow"){

color = yellow;

colors.push_back(color);

}else if(input=="green"){

color = green;

colors.push_back(color);

}else if(input=="blue"){

color = blue;

colors.push_back(color);

}else if(input=="indigo"){

color = indigo;

colors.push_back(color);

}else if(input=="violet"){

color = violet;

colors.push_back(color);

}

}

for(int i=0;i<colors.size();i++){

cout<<colors[i]<<endl;

}

return 0;

}

User Muhammed Anees
by
3.1k points