Problem Comparing Strings to Ints

Teaching myself C++, but I have come across a problem in my book that I cannot get past.

Here it is:

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.


The problem is that the IF / ELSE statements will not compare a String to an Int. Im not sure if i should even use strings, but i have no clue what to do next.

I have looked online to see if anyone has posted an explanation but the only ones i found for this problem were in python.

This is my first post, please dont hurt me. :D

This is my code so far:

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string color1, color2;
	int red, blue, yellow;

	red = 1;
	blue = 2;
	yellow = 3;
	
	cout << "Please choose 2 primary colors (lower case)." << endl;
	
        cout << "Enter color one: ";
	getline(cin, color1);

	cout << "Enter color two: ";
	getline(cin, color2);

	if (color1 == red) // i get a red line under all the == operator.
		if (color2 == blue)
			cout << "Purple" << endl;
		else if (color2 == yellow)
			cout << "Orange" << endl;
	else if (color1 == blue)
		if (color2 == red)
			cout << "Purple" << endl;
		else if (color2 = yellow)
			cout << "Green" << endl;
	else if (color1 == yellow)
		if (color2 = red)
			cout << "Orange" << endl;
		else if (color2 = blue)
			cout << "Green" << endl;
	else
		cout << "Please choose 2 primary colors (lower case)." << endl;
			
	system("pause");
	return 0;
}
The easiest thing would be to just do

1
2
3
4
cout << "Please choose 2 primary colors" << endl;
cout << "[1] red" << endl;
cout << "[2] blue" << endl;
cout << "[3] yellow" << endl;


and have them enter an int


You could also store the strings in an array/vector, loop through it, and select the matching index (or a parallel vector containing the int value for each color, so you won't have to waste the element at index 0). But that's probably something for when you're a bit further along in the book ^
Last edited on
if you want to compare strings to int then this thread is invaluable.
http://www.cplusplus.com/forum/articles/9645/

As for your code, I had a little fun just to see what i could do with it. While there are way better ways of doing this, my way is from a beginners perspective. So hopefully you didn't mind me tinkering on your code.

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
#include <iostream>
#include <algorithm> //using transform function
#include <string>
#include <cctype> // using tolower function
using namespace std;

int main()
{
//didn't see the need to compare int to string. 
	string red, blue, yellow;
	string color1, color2;
	
	cout << "Please choose 2 primary colors (lower case)." << endl;

	cout << "Enter color one: ";
	getline(cin,color1);

//forces user input to all lower
	std::transform(color1.begin(), color1.end(), color1.begin(), ::tolower);
	
	cout << "Enter color two: ";
	getline(cin, color2);
	std::transform(color2.begin(), color2.end(), color2.begin(), ::tolower);

// i tend to use loops for testing 
	while (color1 != "q" && "Q")
	{
		if ((color1 == "red" || color2 == "red") && (color1 == "blue" || color2 == "blue"))
			cout << "Purple" << endl;

//what happens if they put yellow first then red?
		else if (color1 == "red" && color2 == "yellow")
			cout << "Orange" << endl;
		else if (color1 == "yellow" && color2 == "blue")
			cout << "Green" << endl;
		else
			cout << "Invalid Input" << endl;

		cout << "Enter color one: ";
		getline(cin, color1);
		std::transform(color1.begin(), color1.end(), color1.begin(), ::tolower);

		cout << "Enter color two: ";
		getline(cin, color2);
		std::transform(color2.begin(), color2.end(), color2.begin(), ::tolower);

	}
	cout << "Press <ENTER to Exit>";

// better than a system pause
	cin.get();

	return 0;
}
Last edited on
Yeah thanks for the help guys, but I was looking for something simpler.

Since this is a chapter 4 question, using advanced methods of solving it doesn’t help me in the sense that I don’t learn anything.

For example: arrays are in chapter 7 and I don’t even know what these do yet: #include <algorithm> #include <cctype>.

Yes those methods are probably better but I really want to know exactly why it is that my code is not working.

I’m not a student trying to get someone to do my homework. I am just a guy trying to learn c++.

Yes those methods are probably better but I really want to know exactly why it is that my code is not working.

Basically because you can't compare a string to a number. You could however do something like:
1
2
3
4
...
   string color1, color2;
   string red = "red", blue = "blue", yellow = "yellow";
...


But remember C++ is case sensitive, "red" is not equal to "RED" or "Red" or "rEd", etc.
Last edited on
Wetster, I get it. I am working in c++ primer 6th edition and look for responses that fall in-line with what I have covered.

If the textbook does not explicitly state that you need to compare int to string, then don't do it. just compare string to string and learn about comparisons.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string color1, color2;

	cout << "Input color 1: ";
	getline(cin, color1);

	cout << "Input color 2: ";
	getline(cin, color2);

	if (color1 == "red" && color2 == "blue")
	{
		cout << "Those two colors make: Purple" << endl;
	}
	else if (color1 == "blue" && color2 == "yellow")
	{
		cout << "Those two colors make: Green" << endl;
	}
	else
	{
		cout << "Invalid Input....";
	}
	cin.get();
}
closed account (48T7M4Gy)
@OP

Why not use strings for the colors you input? After all that's what you are told to do.

So you can cin >> color1, etc then test each input color1 == "red" etc and allocate color_numbers1 etc so that if color2 is "red" then color_number2 has an integer value of 1 etc, whatever you choose in your design.

That means by adding corresponding integer values and defining numbers for the mixed colors you can find out the color name of the mixed color by working in reverse.

There are 3 primaries and 3 mixed colors so it's just a handful of if/endifs. It doesn't have to be complicated, especially because you are close to it with your program. Trust me you don't need much more than what you have. eg if (color1=="red" etc

Come to think of it you could go even simpler and write:
1
2
if(color1 == "red" && color2=="blue")
   color_mix = "purple"; 

etc
pyos thank you for taking your time to help me !

jlb and kemort gave me the logic behind it !

Thank you DrZoidBerg for your help too. Im sure arrays will be fun to learn.
Last edited on
Topic archived. No new replies allowed.