Answer to Question #298190 in C for NoobMaster

Question #298190

You have to 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.


1
Expert's answer
2022-02-15T13:57:40-0500


#include <stdio.h>
#include <string>
#include <stdlib.h>


void main(){
	int ch;
	char colors[3][100];
	int i=0;
	int j;


	do{
		printf("1. Red\n");
		printf("2. Yellow\n");
		printf("3. White\n");
		printf("4. Green\n");
		printf("5. Blue\n");
		printf("6. Black\n");
		printf("7. Orange\n");


		printf("Select color: ");
		scanf("%d",&ch);


		switch(ch){
		case 1:
			strcpy(colors[i],"Red");
			i++;
			break;
		case 2:
			strcpy(colors[i],"Yellow");
			i++;
			break;
		case 3:
			strcpy(colors[i],"White");
			i++;
			break;
		case 4:
			strcpy(colors[i],"Green");
			i++;
			break;
		case 5:
			strcpy(colors[i],"Blue");
			i++;
			break;
		case 6:
			strcpy(colors[i],"Black");
			i++;
			break;
		case 7:
			strcpy(colors[i],"Orange");
			i++;
			break;
		}


	}while(i<3);


	printf("\nSelected colors:\n");
	for(j=0;j<i;j++){
		printf("%s\n",colors[j]);
	}
	scanf("%d",&ch);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS