Character Color
B or b Blue
G or g Green
R or r Red
W or w White
For example, if the user enters 12b45, the program should display the word “Blue” on the screen. If the item number does not contain exactly five characters, or if the third character is not one of the characters listed above, the program should display an appropriate message on the screen.
I have it where if the users enters an item number less than 5 digits they are prompted to re-enter. I could not figure out how to prompt for the 3 digit as well. Here is what I have so far.
Expand|Select|Wrap|Line Numbers
- /*
- Lab 12
- Displays the color of an item
- */
- #include <iostream>
- #include <string>
- #include <algorithm>
- using std::cout;
- using std::cin;
- using std::endl;
- using std::string;
- int main()
- {
- //declare variables
- string itemNumber = "";
- string letter = "";
- int location = 0;
- //get the item number
- {
- cout << "Please enter a five-digit item number: ";
- getline(cin, itemNumber);
- //location = int(itemNumber.find("x", 3));
- //if (location >= 0)
- if(itemNumber.length() != 5)
- {
- cout << "Sorry, item number must be 5-digits and the 3-digit must be g, b, r or w: ";
- getline(cin, itemNumber);
- transform(itemNumber.begin(), itemNumber.end(), itemNumber.begin(), toupper);
- cout << "Item number " << itemNumber << " is " << endl;
- }
- else
- {
- transform(itemNumber.begin(), itemNumber.end(), itemNumber.begin(), toupper);
- cout << "Item number " << itemNumber << " is " << endl;
- } //end if
- }
- } //end main function