Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Color Mixer The colors red, blue, and yellow are known as the primary colors bec

ID: 671984 • Letter: C

Question

Color Mixer The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Write a program that prompts the user to enter the names of two primary colors to mix. If the user enters anything other than red, blue, or yellow, the program should display an error message. Otherwise, the program should display the name of the secondary color that results by mixing two primary colors.

Explanation / Answer

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
bool flag1, flag2;
string color1, color2, color3;
cout<<"Enter two primary colors to mix: ";
cin>>color1;   //Read color1
cin>>color2;   //Read color2
color3 = "fail";   //color3 initialized to fail
flag1 = color1.compare("red");   //color1 is red or not.
flag2 = color2.compare("blue");   //color2 is blue or not.
if(flag1 == 0 && flag2 == 0)       //If color1 is red and color2 is blue
color3 = "purple";               //Secondary color is purple.
flag1 = color1.compare("blue");   //color1 is blue or not.
flag2 = color2.compare("red");   //color2 is red or not.
if(flag1 == 0 && flag2 == 0)       //If color1 is blue and color2 is red
color3 = "purple";               //Secondary color is purple.
flag1 = color1.compare("red");
flag2 = color2.compare("yellow");
if(flag1 == 0 && flag2 == 0)
color3 = "orange";
flag1 = color1.compare("yellow");
flag2 = color2.compare("red");
if(flag1 == 0 && flag2 == 0)
color3 = "orange";
flag1 = color1.compare("blue");
flag2 = color2.compare("yellow");
if(flag1 == 0 && flag2 == 0)
color3 = "green";
flag1 = color1.compare("yellow");
flag2 = color2.compare("blue");
if(flag1 == 0 && flag2 == 0)
color3 = "green";
  

if(!color3.compare("fail"))   //If any of the conditions did not satisfy.
cout<<"Invalid Primary Color. The primary colors are only RED, BLUE, and YELLOW."<<endl;   //Print ERROR message.
else
cout<<"The name of the secondary color after mixing "<<color1<<" and "<<color2<<" is: "<<color3<<endl; //Print the secondary color.
}