Write a console program: Code the program so that it displays the color of the i
ID: 3803656 • Letter: W
Question
Write a console program: Code the program so that it displays the color of the item whose item number is entered by the user. All the item numbers contain exactly five characters. Allitems are available in four colors: blue, green, red, white. The third character in the item number indicates the item's color, as follows: B or b = Blue G or g = Green R or r = Red W or w = White If the item number does not contain exactly five characters, or the third character is not one of the characters listed above, the program should display an appropriate message on the screen. The program should use a void function to check the item number to determine the color of the item, and the color should be given to the calling function by using a reference variable. Use an array to store the colors and corresponding characters.
Explanation / Answer
Using c++ program code is below regarding colors.
after program execution we are entering either small or big letter i will give corresponding color.
otherwise not matching color message will display.
#include <iostream>
#include <string>
#include <cctype>
using std:: cout;
using std:: cin;
using std:: endl;
//function prototypes
void getItemNumber
void checkItemNumber
int main()
{
//declare variables
string item = " ";
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' or 'b' == toupper(item[2]))
cout << "entered color is blue" << endl;
else if ('G' or 'g' == toupper(item[2]))
cout << "entered color is green" << endl;
else if ('R' or 'r' == toupper(item[2]))
cout << "entered color is red" << endl;
else if ('W' or 'w' == toupper(item[2]))
cout << "entered color is white" << endl;
}
else cout<< "Invalid name no matching color..."; // if code is not from any of the above.
system("pause");
return 0;
}
output will be
we are enter b or B -->entered color is blue
we are enter g or G -->entered color is green
we are enter r or R -->entered color is red
we are enter w or W -->entered color is white
otherwise Invalid name no matching color... will display.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.