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.
#include <iostream>
#include <string>
usingnamespace 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";
elseif (partColor == "G" || "g")
cout << "The part is green";
elseif(partColor == "R" || "r")
cout << "The part is red";
elseif(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
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")
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.
#include <iostream>
#include <string>
usingnamespace 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";
elseif (partColor == "G" || partColor == "g")
cout << "The part is green";
elseif(partColor == "R" || partColor == "r")
cout << "The part is red";
elseif(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
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.