Trying to learn string class...function not working as I anticipated

I know the title is rather ambiguous, and I apologize for that. I can give a better explanation of the problem here. The code posted below is rather straight-forward. I need to get the part number from the user, and based on the letter, display the color associated with it. The program is supposed to take the fourth character and compare it to determine which color to display to the user. (i.e. if the fourth symbol is w, then it should display "White"). However, no matter what you put in, the output is always "The part is blue". At this point, I'm kind of starting to feel like Jim Carrey in Liar Liar, but I figured before I started writing on the walls I would see if maybe someone here could possibly help. And, as always, your help is greatly appreciated.

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

int main ()
{
	//declare variables
	string partNum = "";
	string partColor = "";

	//verify length
	while (partNum != "-1")
	{
		//obtain part number from user
		cout << "What's the part number? Enter -1 to terminate the program and your life ;-)";
		getline(cin, partNum);
		
		//get color code from part number
		partColor = partNum.substr(4,0);
		
		//determine color based on color code and output color
		if (partNum.length() == 7)
		{
			if (partColor == "B" || "b")
			cout << "The part is blue";
				else 
				  if (partColor == "G" || "g")
				  cout << "The part is green";
					else 
					   if(partColor == "R" || "r")
					   cout << "The part is red";
						else
						   if(partColor == "W" || "w")
						   cout << "The part is white";
						else
				cout << "Invalid part number"<< endl;
		}
		else 
			cout << "That ain't right bro";
			cout << endl;
			
	}//end of loop
		system ("pause");
		return 0;
	
}//end of main function

			
Last edited on
if (partColor == "B" || "b")
This will ALWAYS be evaluated to true, because you have the equivalent of (x || true). It converts "b" to the ASCII value of 'b' (or something along those lines, not sure how string literals are converted but it will be non-zero), which is a non-zero integer, and therefore will be considered "true".

tl;dr You need to be more explicit, each term in a boolean statement is its own separate thing
if (partColor == "B" || partColor == "b")
Last edited on
Okay, that actually makes quite a lot of sense. I didnt really think about it that way, and seeing it in that light makes it much more clear. Now, I have gone through and tried to debug the program again, making the correction you suggested, and tweaking some other things, and now the problem is at the other end of the spectrum. That is, now it the loop will not evaluate to true regardless. I figured it was the partColor variable, maybe an issue with the character I was telling it to grab. But I am at a loss right now. I hate to act like such a dead weight, but I have been beating my head over stuff like this for some time, and when Google fails me, I like to ask some people who know whats going on. As always, I appreciate any help you guys can give.


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

int main ()
{
	//declare variables
	string partNum = "";
	string partColor = "";

	while (partNum != "-1")
	{
		//obtain part number from user
		cout << "What's the part number? Enter -1 to terminate the program and your life ;-): ";
		cout << endl;
		cin >> partNum;
		
		//get color code from part number
		partColor = partNum.substr(2,1);
		
		//determine color based on color code and output color
		if (partNum.length() == 7)
		{
			if (partColor == "B" || partColor == "b")
					cout << "The part is blue";
				else 
					if (partColor == "G" || partColor == "g")
				cout << "The part is green";
					else 
						if(partColor == "R" || partColor == "r")
					cout << "The part is red";
						else
							if(partColor == "W" || partColor == "w")
						cout << "The part is white";
								else
							cout << "Invalid part number"<< endl;
		}
		else 
			cout << "That ain't long enough bro";
			cout << endl;
			
	}//end of loop
		system ("pause");
		return 0;
	
}//end of main function
closed account (48T7M4Gy)
If I enter 12w4567 etc the program runs as expected and displays the appropriate colour. It also handles incorrect part numbers.

Well....there is always that chance that I'm just not paying attention or something. I swear, that didnt happen the first time. Well, I appreciate the help with that so very much. Again, you have helped me in a time of dire need. Thank you again.
Topic archived. No new replies allowed.