I am learning how to manipulate strings this week and I am stuck on a program that is suppose to let the user enter a particular item number. The program should then check to see if the entered number is 5 digits long. if its not it should say invalid item number and ask to re enter a valid number. if it is a valid 5 digit number it is then suppose to search the entered number for one of the listed letters if it has "b" or "B" in the 3rd spot it should display your color is blue. "w" or "W" should display your color is white. It is also suppose to display invalid item no matching color if there isnt a matching 3rd letter. Hope this makes sense
I am getting the correct error messages. The problem I have is it returns the color is blue no matter if i enter "45b90" "89G90" "89w90" if what i enter is true everywhere else the answer is always blue no matter. Why is that?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string item = "";
cout << "Enter your 5-digit item #: ";
cin >> item;
transform(item.begin(), item.end(), item.begin(), toupper);
while (item.length() != 5)
{
cout << "Invalid item #. Please enter a 5-digit item # ";
cin >> item;
transform(item.begin(), item.end(), item.begin(), toupper);
}
if (item.length() == 5)
{
if (item.find("B", 0))
cout << "Your color is blue" << endl << endl;
else if (item.find("G", 0))
cout << "Your color is green" << endl << endl;
else if (item.find("R", 0))
cout << "Your color is red" << endl << endl;
else if (item.find("W", 0))
cout << "Your color is white" << endl << endl;
}
else
cout<< "Invalid item no matching color..."; // if code is not from any of the above.
return 0;
}