Question

Write an interactive C++ program that asks a user to input the color code of a...

Write an interactive C++ program that asks a user to input the color code of a resistor and determines its value and tolerance

  • Tell the user how to input the color rings “Instructions”
  • Tell the user to input the colors one after the other
  • After the program receives the information it will display the resistance and tolerance
  • Output an error message if the user enters an invalid color code (no garbage values should be displayed), and ask for another input after every display
  • Repeat asking for the color code of another resistor until the user says that he/she is done and wants to exit; meaning the program is going to have different options

How to read 4 Color Resistor Codes

First the code

Black

Brown

Red

Orange

Yellow

Green

Blue

Violet

Gray

White

0

1

2

3

4

5

6

7

8

9

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include<map>
using namespace std;
int main()
{
   int total = 0;
   map<string, int>color;//data structure which stores a string value as key and int value as its value
   color["Black"] = 0;//initialising map
   color["Brown"] = 1;
   color["Red"] = 2;
   color["Orange"] = 3;
   color["Yellow"] = 4;
   color["Green"] = 5;
   color["Blue"] = 6;
   color["Violet"] = 7;
   color["Gray"] = 8;
   color["White"] = 9;
   while(1)//run infinitely
   {
       int total = 0;//to calculate total resistance for a resistor
       for(int i = 0; i < 4 ; i++)//to input 4 resistors
       {
           while(1)
           {
               cout<<"Enter colour to calculate resistance: ";
               string col;cin>>col;
               if(color.find(col) == color.end())//find method returns the position of col in map, end() means that the value is not present in the map
               {
                   cout<<"Enter a valid color"<<endl;
               }
               else
               {
                   total += color[col];
                   break;//valid color, so need not read again
               }
           }
       }
       cout<<"Total Resistance: "<<total<<" ohms"<<endl;
       cout<<"Enter -1 to break, any other key to continue: ";
       string ex;cin>>ex;if(ex=="-1") break;
   }
   return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write an interactive C++ program that asks a user to input the color code of a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the...

    Write an interactive Java program, ColorRange.java, which when given a wavelength in nanometers will return the corresponding color in the visible spectrum. Color Wavelength (nm) Violet 380-450 Blue 451-495 Green 496-570 Yellow 571-590 Orange 591-620 Red 621-750 Task You must implement the following using a suitable decision statement. 1. Use a GUI prompt for the user to enter the wavelength, the wavelength should be of type double. 2. For each range (e.g. 380-450) the number on the left is included...

  • Three resistors with color codes 1. Brown, Brown, Brown, 2. Red, Red, Red 3. Orange, Black,...

    Three resistors with color codes 1. Brown, Brown, Brown, 2. Red, Red, Red 3. Orange, Black, Orange are connected in parallel. A battery of voltage 4.5 V is connected to the resistors. a) Draw the circuit diagram. b) List the values of the resistors according to color code. c) Calculate the equivalent resistance 1st Digit d) Find the current through each resistor e) Add the currents through each resistor to find the total current and use the voltage to find...

  • Design and implement a C program that acts as a resistor color code encoder using 4 color bands. for example, a 1.2k resistance with 2% tolerance is expressed as [1] black [2] red [3] brown [4] red in...

    Design and implement a C program that acts as a resistor color code encoder using 4 color bands. for example, a 1.2k resistance with 2% tolerance is expressed as [1] black [2] red [3] brown [4] red in a 4 color band resistor. input: Resistance, R, in ohms or kilo-ohms output: The color code sequence the user should not be asked to do the conversion. Do not ask the user to enter the first number, the second number, multiplication factor...

  • write a c++ code be sure the user only enter A or a for packageA, B...

    write a c++ code be sure the user only enter A or a for packageA, B or b for packageB, C or c for packageC. If the user inputs some other value display an error message saying " invalid out". When you ask the user to input the number of minute and the user enters a negative value you should display the same error message "invalid input" in both cases, after you display the error message the program should stop...

  • Please help me with this question! It is due by midnight! I'm doing this in Visual...

    Please help me with this question! It is due by midnight! I'm doing this in Visual Studio 2019. But any others are fine as long as it can compile. I also wanted to see the results if it is compiled and QT comments as well. Please respond immediately! Description: For this homework we will use classes to model something in the real world. In this case a resistor. The colored bands on the top-most resistor shown in the photo below...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • Write a Python program that asks the user to enter the number of calories and fat...

    Write a Python program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from fat. One gram of fat has 9 calories, therefore: Calories from fat = fat grams * 9 The percentage of calories from fat can be calculated as follows: Calories from fat / total calories If the calories from fat are less than 30 percent of the total...

  • Write a java program that uses a loop to input, from the user not using gui,...

    Write a java program that uses a loop to input, from the user not using gui, a collection of values that are the number of steps taken each day for a sequence of days and stores those amounts in an array. the array length should be 31, The user is not required to enter 31 values. the program determines and displays the largest number of steps for a single day, the smallest number of steps for a single day and...

  • C++ Write a program that asks user to input three positive integers one at a time,...

    C++ Write a program that asks user to input three positive integers one at a time, then compute and output the largest entered number. Use if-else statements for three integer comparison and use for loops for a user validation. Example output: Enter an integer: -7 Invalid! Number must be positive. Enter an integer: -2 Invalid! Number must be positive. Enter an integer: 5 Enter an integer: 7 Enter an integer: 1 Largest number: 7

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT