Here is where I am at. This code compiles and works fine except for two issues. First -1 does not terminate the program. Second, if you enter 7 characters without the 4th being a B, G, R, or W, it does not tell you Invalid Item Description.
You can make the main function return (and thus your programme end) with the statement return 0;
Put that between the braces I marked in bold, so that it is executed when item == "-1"
You want it to say "invalid item" if none of the four colours come up? Simply add a final else statement to the set of if else statements checking for the right colour.
1 2 3 4 5 6 7 8 9 10 11 12 13
if (item.length() == 7)
{
if('B' == toupper(item[3]))
cout << "Your item's color is blue" << endl;
elseif ('G' == toupper(item[3]))
cout << "Your item's color is green" << endl;
elseif ('R' == toupper(item[3]))
cout << "Your item's color is red" << endl;
elseif ('W' == toupper(item[3]))
cout << "Your item's color is white" << endl;
else
{cout<< "Invalid item description - no matching color.";}
}
Currently, your code asks the user to enter his item, and then immediately asks again at the start of the while loop. That seems rather inelegant; does
1 2 3 4
else {
cout << "Enter your 7-character item description (-1 to quit): ";
getline(cin, item);
}
need to be there? Note that the second time you ask for the item, entering "-1" does not make it quit, even though it says it will.
Adding the else statement fixed the "no matching color" problem. But I am still stuck on getting the program to quit when someone enters a -1. Here is the revised code:
So what is the code that goes between the braces to make the program quit? I've been at this for four hours now and I am tired, unable to figure it out, lost.
There's not a week goes by that I don't open some C++ for maintenance and have a panic attack. Once they had to coax me back out from under my desk with a doughnut :)