Write a menu-driven program that asks the user to select a favorite color from the given colors. Keep asking the user three times for selecting the color from the list of the available colors. Do this with the help of a suitable loop and switch statement. Finally, you have to display the three favorite colors of the user.
#include <iostream>
#include <string>
using namespace std;
int main(){
string selectedColors[3];
int selectedColorCount=0;
do{
cout<<"1. Red\n";
cout<<"2. Yellow\n";
cout<<"3. White\n";
cout<<"4. Green\n";
cout<<"5. Blue\n";
cout<<"6. Black\n";
cout<<"7. Orange\n";
int ch;
cout<<"Select color: ";
cin>>ch;
switch(ch){
case 1:
selectedColors[selectedColorCount]="Red";
selectedColorCount++;
break;
case 2:
selectedColors[selectedColorCount]="Yellow";
selectedColorCount++;
break;
case 3:
selectedColors[selectedColorCount]="White";
selectedColorCount++;
break;
case 4:
selectedColors[selectedColorCount]="Green";
selectedColorCount++;
break;
case 5:
selectedColors[selectedColorCount]="Blue";
selectedColorCount++;
break;
case 6:
selectedColors[selectedColorCount]="Black";
selectedColorCount++;
break;
case 7:
selectedColors[selectedColorCount]="Orange";
selectedColorCount++;
break;
}
}while(selectedColorCount<3);
cout<<"\nSelected colors:\n";
for(int i=0;i<selectedColorCount;i++){
cout<<selectedColors[i]<<"\n";
}
cin>>selectedColorCount;
return 0;
}
Comments
Leave a comment