In: Computer Science
Write a program that does the following things: in C++
1 ) ask the user for 2 primary colors (red, blue, yellow) [should be in main]
2 ) determine the secondary color the 2 primarily colors make [should be in newColor function]
red + blue = purple
red + yellow = orange
blue + yellow = green
3 ) Print the following message
<color1> and <color2> makes <color3>
The Program in C++ for mixing of two primary colors to make third is as follows:
Comments are also included for better understanding.
#include <iostream>
using namespace std;
int main()
{
// Variables
string color_1, color_2;
// Ask user for 2 primary colors
cout << endl;
cout << "Enter 1st primary color : ";
cin >> color_1;
cout << "The 2nd primary color is: ";
cin >> color_2;
// Format line break
cout << endl;
// Error checking, calculation, & Display.
// Eliminating confusion between upper case and lower case.
// define the result for mixer of two primary colors.
if (color_1 == "red" || color_1 == "Red" || coior_1 == "RED")
{
if (color_2 == "blue" || color_2 == "Blue" || color_2 == "BLUE")
{
cout << color_1 << " and ";
cout << color_2 << " makes purple.\n";
}
else if (color_2 == "yellow" || color_2 == "Yellow" || color_2 == "YELLOW")
{
cout << color_1 << " and ";
cout << color_2 << " makes Orange.\n";
}
}
else if (color_1 == "blue" || color_1 == "Blue" || color_1 == "BLUE")
{
if (color_2 == "red" || color_2 == "Red" || color_2 == "RED")
{
cout << color_1 << " and ";
cout << color_2 << " makes purple.\n";
}
else if (color_2 == "yellow" || color_2 == "Yellow" || color_2 == "YELLOW")
{
cout << color_1 << " and ";
cout << color_2 << " makes green.\n";
}
}
else if (color_1 == "yellow" || color_1 == "Yellow" || color_2 == "YELLOW")
{
if (color_2 == "red" || color_2 == "Red" || color_2 == "RED")
{
cout << color_1 << " and ";
cout << color_2 << " makes orange.\n";
}
else if (color_2 == "blue" || color_2 == "Blue" || color_2 == "BLUE")
{
cout << color_1 << " and ";
cout << color_2 << " makes green.\n";
}
}
else
{
cout << "Neither " << color_1;
cout << " nor " << color_2;
cout << " is a prime color. ";
cout << "\nRun the program and try again.\n";
cout << "Remember a primary color is either Red,";
cout << "Blue, or Yellow.\n";
}
// Formatting
cout << endl << endl;
// Terminate program
return 0;
}