Answer to Question #295900 in C++ for Nickname

Question #295900

You work in a printing office that the machine there can print documents in different colors. It gets an integer number between 1 to 7 and prints the documents in rainbow colors, as shown in the following table:

Number Matched Color

1 Violet

2 Indigo

3 Blue

4 Green

5 Yellow

6 Orange

7 Red


Design an algorithm and write a C++ program to do the following:

  • Prompt user to enter a number between 1 and 7
  • Use selection structure such as if-else or switch to find the color based on the previous table
  • Output the color related to input number. See the sample output.
  • Save the output in a file named “color.txt”.

Sample Run

Enter a number between 1 and 7: 2

You have selected number 2 which is orange.


Output File "color.txt"

You have selected number 2 which is orange.

Here are the numbers and match colors:

1 ------- Violet

2 ------- Indigo

3 ------- Blue

4 ------- Green

5 ------- Yellow

6 ------- Orange

7 ------- Red


1
Expert's answer
2022-02-10T09:49:30-0500




Algorithm:




Start
	Declare Integer selectedColor
	Declare String selectedColorString
	Display "Number Matched Color"
	Display "1 Violet"
	Display	"2 Indigo"
	Display "3 Blue"
	Display "4 Green"
	Display "5 Yellow"
	Display "6 Orange"
	Display "7 Red"
	while selectedColor<1 or selectedColor>7 
		Display "Enter a number between 1 and 7: "
		Input selectedColor
	End while
	switch selectedColor
	case 1: Set selectedColorString="violet"
	case 2: Set selectedColorString="indigo"
	case 3: Set selectedColorString="blue"
	case 4: Set selectedColorString="green"
	case 5: Set selectedColorString="yellow"
	case 6: Set selectedColorString="orange"
	case 7: Set selectedColorString="red"
	default: Display "Wrong color"
	if selectedColor>0 and selectedColor<8 then
		Display "You have selected number ",selectedColor," which is ",selectedColorString,"."
		open file "color.txt"
		Save to the file the message: "You have selected number ",selectedColor," which is ",selectedColorString,"."
		Save to the file the message: "Here are the numbers and match colors:"
		Save to the file the message: "1 ------- Violet"
		Save to the file the message: "2 ------- Indigo"
		Save to the file the message: "3 ------- Blue"
		Save to the file the message: "4 ------- Green"
		Save to the file the message: "5 ------- Yellow"
		Save to the file the message: "6 ------- Orange"
		Save to the file the message: "7 ------- Red"
		close the file
	end if
Stop


C++ program


#include <iostream>
#include <string>
#include <fstream>


using namespace std;
int main(){


	int selectedColor=-1;
	string selectedColorString;
	cout<<"Number Matched Color\n";
	cout<<"1 Violet\n";
	cout<<"2 Indigo\n";
	cout<<"3 Blue\n";
	cout<<"4 Green\n";
	cout<<"5 Yellow\n";
	cout<<"6 Orange\n";
	cout<<"7 Red\n";
	while(selectedColor<1 || selectedColor>7){
		cout<<"Enter a number between 1 and 7: ";
		cin>>selectedColor;
	}
	switch(selectedColor){
	case 1:
		selectedColorString="violet";
		break;
	case 2:
		selectedColorString="indigo";
		break;
	case 3:
		selectedColorString="blue";
		break;
	case 4:
		selectedColorString="green";
		break;
	case 5:
		selectedColorString="yellow";
		break;
	case 6:
		selectedColorString="orange";
		break;
	case 7:
		selectedColorString="red";
		break;
	default:
		cout<<"Wrong color\n\n";
		break;
	}
	if(selectedColor>0 && selectedColor<8){
		cout<<"You have selected number "<<selectedColor<<" which is "<<selectedColorString<<".\n\n";
		ofstream file;
		file.open("color.txt");
		file<<"You have selected number "<<selectedColor<<" which is "<<selectedColorString<<".\n";
		file<<"Here are the numbers and match colors:\n";
		file<<"1 ------- Violet\n";
		file<<"2 ------- Indigo\n";
		file<<"3 ------- Blue\n";
		file<<"4 ------- Green\n";
		file<<"5 ------- Yellow\n";
		file<<"6 ------- Orange\n";
		file<<"7 ------- Red\n";
		file.close();
	}


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog