if/else statement: getting an error message I don't understand

I'm supposed to write a code that asks a user to input two primary colors (red, blue or yellow) and then tell the user what new color those two primary colors mixed together form. I feel like what I have is correct but keep getting an error message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//This program obtains two colors and determines what color they make when mixed
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char** argv) 
{
	//Vars...
	string color1, color2;
	
	//Ask the user for two primary colors
	cout << "Please enter two primary colors (red, blue, yellow), in lowercase, separated by a space: ";
	cin >> color1;
	cin >> color2;
	
	//Find out what the user entered
	if (color1 == "red")
	{
		if (color2 == "blue")
			{
				cout << "Red and blue mix to form purple." << endl;
			}
			else if (color2 == "yellow")
				{
				cout << "Red and yellow mix to form orange." << endl;
				}
	}
	else if (color1 == "blue")
	{
		if (color2 = "red")
			{
				cout << "Blue and red mix to form purple." << endl;
			}
			else if (color2 = "yellow")
				{
					cout << "Blue and yellow mix to form green." << endl;
				}
	}
	else if (color1 = "yellow")
	{
		if (color2 = "red")
			{
				cout << "Yellow and red mix to form orange." << endl;
			}
		else if (color2 = "blue")
			{
				cout << "Yellow and blue mix to form green. << endl;
			}
	}
	else
	{
		cout << "That is not a listed color! Please run the program again, entering red, blue, or yellow!" << endl;
	}
	
	return 0;
} 


With the error message being:
[Error] could not convert 'color2.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"red"))' from 'std::basic_string<char>' to 'bool'

On line 21: if (color2 == blue)
On lines 32, 36, 41, 43, and 47 you used = (assignment) rather than == (equality comparison).
Oh my gosh I completely glanced over that I feel stupid lol, thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

int main()
{
    const int RED = 1, BLUE = 2, YELLOW = 4 ;

	std::string color1, color2;
	std::cout << "Please enter two primary colors (red, blue, yellow), in lowercase, separated by a space: ";
	std::cin >> color1 >> color2 ;

	int clr_val1 = -100, clr_val2 = -100;

	if( color1 == "red" ) clr_val1 = RED ;
	else if( color1 == "blue" ) clr_val1 = BLUE ;
	else if( color1 == "yellow" ) clr_val1 = YELLOW ;

	if( color2 == "red" ) clr_val2 = RED ;
	else if( color2 == "blue" ) clr_val2 = BLUE ;
	else if( color2 == "yellow" ) clr_val2 = YELLOW ;

	std::cout << color1 << " + " << color2 << " == " ;

	switch( clr_val1 + clr_val2 )
	{
	    case RED+RED : std::cout << "red\n" ; break ; // 1+1 == 2
	    case RED+BLUE : std::cout << "purple\n" ; break ; // 1+2 == 3
	    case RED+YELLOW: std::cout << "orange\n" ; break ; // 1+4 == 5
	    case BLUE+BLUE : std::cout << "blue\n" ; break ; // 2+2 == 4
	    case BLUE+YELLOW : std::cout << "green\n" ; break ; // 2+4 == 6
	    case YELLOW+YELLOW: std::cout << "yellow\n" ; break ; // 4+4 == 8
	    default: std::cout << " *** error *** invalid primary colour\n" ;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <map>
using namespace std;


   string intToColour[] = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" };       // light (RGB)
// string intToColour[] = { "white", "cyan", "magenta", "blue", "yellow", "green", "red", "black" };       // paint (CMY)
   map<string,int> colourToInt;


bool validPrimary( string colour )
{
   auto it = colourToInt.find( colour );
   if ( it == colourToInt.end() ) return false;
   int i = it->second;
   return ( i == 1 || i == 2 || i == 4 );
}


int main()
{
   int i = 0;
   for ( string s : intToColour ) colourToInt[s] = i++;    // Invert mapping

   string col1, col2, result;
   cout << "Enter primary colours in lower case: ";
   cin >> col1 >> col2;

   if ( !validPrimary( col1 ) || !validPrimary( col2 ) ) result = "Invalid";
   else if ( col1 == col2 )                              result = col1;
   else                                                  result = intToColour[ colourToInt[col1] + colourToInt[col2] ];

   cout << "Result: " << result << '\n';
}
Topic archived. No new replies allowed.