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;
}

well figured it out on my own

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
string item = "";

cout << "Enter your 5-digit item #: ";
cin >> item;

while (item.length() != 5)
{
	cout << "Invalid item #. Please enter a 5-digit item # ";
	getline(cin, item);
}

if (item.length() == 5)
{
	if('B' == toupper(item[2])) 
	cout << "Your color is blue" << endl;
	else if ('G' == toupper(item[2])) 
	cout << "Your color is green" << endl;
	else if ('R' == toupper(item[2]))
	cout << "Your color is red" << endl;
	else if ('W' == toupper(item[2]))
	cout << "Your color is white" << endl;
}
else
	cout<< "Invalid item no matching color..."; // if code is not from any of the above.

return 0;
}

Why were you using transform ?

It was the only example I could find in my text book. Found other info from searching how to do it how I ended up. I realized after sending it in there is actually still one error in it. Oh well its working way better than initially :P

Also because I am a beginning c++ user and struggling big time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.