I am learning C++ and would like to make cleaner code. I am learning on Udemy.com how having the main at the bottom of the program helps when making blocks of code. What I would like to do is take some code I already have, and have the user enter two prime colors to make a mixed color, then ask the user if they would like to mix another color with two of the same prime and one other prime color to get: yellow-green, yellow-orange, orange-red, red-purple, blue-green, and blue-purple.  Is my logic flawed? What is the correct logic? How would I set up this code to have different functions outside the main with the main at the bottom? I know it is supposed to read the information first somehow. I just can't process how to write it.

icon
Related questions
icon
Concept explainers
Question

I am learning C++ and would like to make cleaner code. I am learning on Udemy.com how having the main at the bottom of the program helps when making blocks of code.

What I would like to do is take some code I already have, and have the user enter two prime colors to make a mixed color, then ask the user if they would like to mix another color with two of the same prime and one other prime color to get: yellow-green, yellow-orange, orange-red, red-purple, blue-green, and blue-purple. 

Is my logic flawed? What is the correct logic? How would I set up this code to have different functions outside the main with the main at the bottom? I know it is supposed to read the information first somehow. I just can't process how to write it.

#include <iostream>
#include <string>

using namespace std;

int main() {
    string arr[] = {"red", "blue", "yellow"};
    int color1, color2;
    string mixedColor;
    char choice;

    do {
        cout << "Enter one prime color followed by a second prime color: "
        << "'0' for red, '1' for blue, and '2' for yellow: ";
        
        cin >> color1 >> color2;

        if ((color1 == 0 && color2 == 1) || (color1 == 1 && color2 == 0)) {
            mixedColor = "purple";
        } else if ((color1 == 0 && color2 == 2) || (color1 == 2 && color2 == 0)) {
            mixedColor = "orange";
        } else if ((color1 == 1 && color2 == 2) || (color1 == 2 && color2 == 1)) {
            mixedColor = "green";
        } else {
            mixedColor = "No Mix";
        }

        // Display results
        cout << "Mixed color for your selection (" << arr[color1] << " and " << arr[color2] << ") is " << mixedColor << endl;

        // Continue or exit?
        cout << "Would you like to mix again? (Y/N): ";
        cin >> choice;
    } while (toupper(choice) == 'Y');

    cout << "Goodbye!" << endl;

    return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I want to keep the array for this mixer and came up with this solution.

#include <iostream>
#include <string>
 
using namespace std;
 
string mixColors(int color1, int color2) {
string mixedColor;
 
        if ((color1 == 0 && color2 == 1) || (color1 == 1 && color2 == 0)) {
            mixedColor = "purple";
        } else if ((color1 == 0 && color2 == 2) || (color1 == 2 && color2 == 0)) {
            mixedColor = "orange";
        } else if ((color1 == 1 && color2 == 2) || (color1 == 2 && color2 == 1)) {
            mixedColor = "green";
        } else {
            mixedColor = "No Mix";
        }
return mixedColor;
}
 
int getUserInput() {
int color;
 
cout << "Enter a prime color: " << "'0' for red, '1' for blue, and '2' for yellow: ";
cin >> color;
 
    return color;
}
 
int main() {
    string arr[] = {"red", "blue", "yellow"};
    int color1, color2;
    string mixedColor;
    char choice;
 
        do {
        color1 = getUserInput();
        color2 = getUserInput();
    mixedColor = mixColors(color1, color2);
 
cout << "Mixed color for your selection (" << color1 << "  and " << color2 << ") is " << mixedColor << endl;
 
        cout << "Would you like to mix again? (Y/N): ";
        cin >> choice;
} while (toupper(choice) == 'Y');
 
cout << "Goodbye!" << endl;
 
return 0;
}
 
Why 'color' used inside the function getUserInput for the cin statement?
And why does this prompt the question to be asked twice be the user?
C:\Users\jerme\Workspaces\L X +
Enter a prime color: '0' for red, '1' for blue, and '2' for yellow: 1
Enter a prime color: '0' for red, '1' for blue, and '2' for yellow: 2
Mixed color for your selection (1
and 2) is green
Would you like to mix again? (Y/N): |
Transcribed Image Text:C:\Users\jerme\Workspaces\L X + Enter a prime color: '0' for red, '1' for blue, and '2' for yellow: 1 Enter a prime color: '0' for red, '1' for blue, and '2' for yellow: 2 Mixed color for your selection (1 and 2) is green Would you like to mix again? (Y/N): |
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, data-structures-and-algorithms and related others by exploring similar questions and additional content below.