Question
Hello, i'm writing a program in C++ for a color value/ ohm value resistor code. I'm having trouble cause I need the code that i
Hello, i'm writing a program in C++ for a color value/ ohm value resistor code. I'm having trouble cause I need the code that i wrote to run when choice A is selected and the other one to run when choice B is selected. I can't get it together, can someone please help? The code is below that i need help fixing. Thanks!
#include
void band3_to_color(int); void tolerance_to_color(float);
char answer; char choice; char unit;
double bands; double resistance;
int color(string[], const int); double time(string[], double[], const int, int, int, int); int tolerance(string[], double[], const int);
int main () {
char first(100); char last (20); // asks the user for first and last and displays it.
cout << "please enter you first name: ";//promts you to enter your first name cin >> first; cin.clear(); //clear extra character in cin cin.ignore(255,' '); //ignore cout << "please enter you last name: ";// promts you to enter your last name. cin >> last; cin.clear(); cin.ignore(255,' '); system ("cls");
cout << "Hello " < { // if statements to choose between a volor value or ohm value cout << "Choose (A) for color value or (B) for ohm value" << endl; cin >> choice; if (choice == 'A' || choice == 'a') { float r = 0.0f; float t = 0.00f; int exp = 0; cout << "Enter Resistance: "; cin >> r; cout << "Enter Tolerance in Decimal Form: "; cin >> t; exp = log10( r / 10.f ); band3_to_color(r / pow(10,exp+1)); band3_to_color( (int( r / pow(10,exp ))) % 10); band3_to_color(exp); tolerance_to_color(t); return 0; } void band3_to_color(int i){ if(i == 0) {cout << "| Black | ";} else if(i == 1) {cout << "| Brown | ";} else if(i == 2) {cout << "| Red | ";} else if(i == 3) {cout << "| Orange | ";} else if(i == 4) {cout << "| Yellow | ";} else if(i == 5) {cout << "| Green | ";} else if(i == 6) {cout << "| Blue | ";} else if(i == 7) {cout << "| Violet | ";} else if(i == 8) {cout << "| Gray | ";} else {cout << "| White | ";} } void tolerance_to_color(float i){ if (i == .1){cout << "| Silver | ";} else if(i == .05){cout << "| Gold | ";} else if(i == .01){cout << "| Brown | ";} else if(i == .02){cout << "| Red | ";} else if(i == .005){cout << "| Green | ";} else if(i == .0025){cout << "| Blue | ";} else {cout << "| Purple | ";} } else if (choice == 'B' || choice == 'b') { cout << "B: Ohm Value" << endl; cout << "Enter a value of 3,4,5 for the number of bands on the resistor." << " "; cin >> bands; cout << "You chose " << bands << " bands" << " "; }} // Variables const int band = 10, multiplier = 12, toleranceSize = 4; // Represents size of each string array int a, b, c; // Holds the 3 colors from band which are entered by user int output; // Contains tolerance double product; // Stores result from calculation // Defined string arrays string BandColor[band] = { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }; string MultiplierColor[multiplier] = { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white", "gold", "silver" }; string ToleranceColor[toleranceSize] = { "brown", "red", "gold", "silver" }; // Arrays which hold numeric values for the string arrays double multiplierArray[multiplier] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0.1, 0.01 }; double toleranceArray[toleranceSize] = { 1, 2, 5, 10 }; // Loop to show each color and the number it corresponds to for (int i = 0; i < band; i++) { cout << BandColor[i] << " == " << multiplierArray[i] << endl; } // Call the function three times and store the information (i.e.: color and its value) in the variables a, b, and c cin.ignore(256,' '); a = color(BandColor, band); b = color(BandColor, band); c = color(BandColor, band); cout << " "; //Call the function to store the result from the calculation product = time(MultiplierColor, multiplierArray, multiplier, a, b, c); cout << " "; //Call the function to store the tolerance output = tolerance(ToleranceColor, toleranceArray, toleranceSize); cout << " "; //Display information about the resistor cout << "This resistor has " << product << " ohms with " << output << " % tolerance." << endl; cout << " "; system("PAUSE"); return 0; } int color(string BandColor[], const int band) { // Variables char code[10]; // Represents the colors from the string array BAND_COLOR_CODE int num = 0; // Holds numeric value from 0-9 for color band static int p = 0; // Stores user input for color ++p; // Increments to allow user to input colors in succession cout << " "; // Prompt for user input cout << "Enter a color for band " << p << ": = "; cin.getline(code, 10); // Loop to take care of the case of letters for (int i = 0; i < 10; i++) code[i] = tolower(code[i]); // Loop to set user input to a number for (int i = 0; i < band; i++) { if(code == BandColor[i]) { switch (i) { case 0: { num = 0; break; } case 1: { num = 1; break; } case 2: { num = 2; break; } case 3: { num = 3; break; } case 4: { num = 4; break; } case 5: { num = 5; break; } case 6: { num = 6; break; } case 7: { num = 7; break; } case 8: { num = 8; break; } case 9: { num = 9; break; } default: { cout << "ERROR!" << endl; } } } } return num; } double time(string MultiplierColor[], double multiplierArray[], const int multiplier, int a, int b, int c) { // Variables char code[10]; // Represents the colors from the string array BAND_COLOR_CODE double total = 0; // Overall sum of a, b, and c double num = 0; // Holds numeric values for multiplier double value; // Stores value of resistor // Loop to show colors in the multiplier for (int i = 0; i < multiplier; i++) { cout << MultiplierColor[i] << " == " << multiplierArray[i] << endl; } cout << " "; // Prompt for user input cout << "Enter a color for the multiplier: = "; cin.getline(code, 10); // Loop to take care of case for letters for (int i = 0; i < 10; i++) code[i] = tolower(code[i]); // Loop to set user input to a number for (int i = 0; i < multiplier; i++) { if (code == MultiplierColor[i]) { switch (i) { case 0: { num = 0; break; } case 1: { num = 1; break; } case 2: { num = 2; break; } case 3: { num = 3; break; } case 4: { num = 4; break; } case 5: { num = 5; break; } case 6: { num = 6; break; } case 7: { num = 7; break; } case 8: { num = 8; break; } case 9: { num = 9; break; } case 10: { num = 0.1; break; } case 11: { num = 0.01; break; } default: { cout << "ERROR!" << endl; } } } } total += (a * 100) + (b * 10) + c; value = total * pow(10, num); return value; } int tolerance(string ToleranceColor[], double toleranceArray[], const int toleranceSize) { // Variables char code[10]; // Represents the colors from the string array BAND_COLOR_CODE int num = 0; // Holds numeric values for tolerance // Loop to set user input to a number for (int i = 0; i < toleranceSize; i++) { cout << ToleranceColor[i] << " == " << toleranceArray[i] << endl; } cout << " "; // Prompt for user input cout << "Enter a color for the tolerance: = "; cin.getline(code, 10); // Loop to take care of case for letters for (int i = 0; i < 10; i++) code[i] = tolower(code[i]); // Loop to set user input to a number for (int i = 0; i < toleranceSize; i++) { if (code == ToleranceColor[i]) { switch (i) { case 0: { num = 1; break; } case 1: { num = 2; break; } case 2: { num = 5; break; } case 3: { num = 10; break; } default: { cout << "ERROR!" << endl; } } } } return num;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started