Question

Color. Write a program that displays the color of the camera whose item number is entered...

Color. Write a program that displays the color of the camera whose item number is entered by the user. All item numbers contain exactly seven characters. All items are available in four colors: black, green, red, and white. The fourth character in the item number indicates the item’s color, as follows: a B or b indicates Black, a G or g indicates Green, an R or r indicates Red, and a W or w indicates White. If the item number does not contain exactly seven characters, or if the fourth character is not one of the valid color characters, the program should display an appropriate message. Use a sentinel value to end the program. Save and then run the program. Test the program using the following item numbers: 123B567, 111r222, 123, and 111k456. Demonstrate your testing in your submission.

Homework Answers

Answer #1

Code:

#include <iostream>

#include <string>

using namespace std;

int main() {

    

    do {

        string item = "";

        cout << "\nEnter your 5-digit itme # or -1 to exit: ";

        cin >> item;

        if (item.length() == 7) {

            if ('B' == toupper(item[3]))

                cout << "Your color is black" << endl;

            else if ('G' == toupper(item[3]))

                cout << "Your color is green" << endl;

            else if ('R' == toupper(item[3]))

                cout << "Your color is red" << endl;

            else if ('W' == toupper(item[3]))

                cout << "Your color is white" << endl;

            else {

                cout << "Invalid item #!!" << endl;

                continue;

            }

        }

        else if (item == "-1")

            break;

        else {

            cout << "Invalid item #!!" << endl;

            continue;

        }

    } while (1);

    return 0;

}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that prompts the user to enter a string and displays the number of...
Write a program that prompts the user to enter a string and displays the number of characters it contains, fourth character, and last character. Note: The string may contain blanks. For example: “C++ programming is fun”. You should write a complete program.
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...