Answer to Question #295461 in C++ for Nickname

Question #295461

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:38-0500


Algorithm:




Start
	Declare Integer sc
	Declare String scs
	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 sc<1 or sc>7 
		Display "Enter a number between 1 and 7: "
		Input sc
	End while
	switch sc
	case 1: Set scs="violet"
	case 2: Set scs="indigo"
	case 3: Set scs="blue"
	case 4: Set scs="green"
	case 5: Set scs="yellow"
	case 6: Set scs="orange"
	case 7: Set scs="red"
	default: Display "Wrong color"
	if sc>0 and sc<8 then
		Display "You have selected number ",sc," which is ",scs,"."
		open file "color.txt"
		Save to the file the message: "You have selected number ",sc," which is ",scs,"."
		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 sc=-1;
	string scs;
	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(sc<1 || sc>7){
		cout<<"Enter a number between 1 and 7: ";
		cin>>sc;
	}
	switch(sc){
	case 1:
		scs="violet";
		break;
	case 2:
		scs="indigo";
		break;
	case 3:
		scs="blue";
		break;
	case 4:
		scs="green";
		break;
	case 5:
		scs="yellow";
		break;
	case 6:
		scs="orange";
		break;
	case 7:
		scs="red";
		break;
	default:
		cout<<"Wrong color\n\n";
		break;
	}
	if(sc>0 && sc<8){
		cout<<"You have selected number "<<sc<<" which is "<<scs<<".\n\n";
		ofstream colorFile;
		colorFile.open("color.txt");
		colorFile<<"You have selected number "<<sc<<" which is "<<scs<<".\n";
		colorFile<<"Here are the numbers and match colors:\n";
		colorFile<<"1 ------- Violet\n";
		colorFile<<"2 ------- Indigo\n";
		colorFile<<"3 ------- Blue\n";
		colorFile<<"4 ------- Green\n";
		colorFile<<"5 ------- Yellow\n";
		colorFile<<"6 ------- Orange\n";
		colorFile<<"7 ------- Red\n";
		colorFile.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