Question: I want to fix this code in C++ to ask me again for the same thing if i put 'y'. #include #include #include #include //

I want to fix this code in C++ to ask me again for the same thing if i put 'y'.

#include #include #include #include

// resister bands enum resistor_band_items {BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE, UNKNOWN};

//constants for min/max colour arguments and max colour char enum {MINC = 3, MAXC = 4, MAXCC = 8}; struct items { char *name; enum resistor_band_items id; } item_list[] = { {"black", BLACK}, {"brown", BROWN}, {"red", RED}, {"orange", ORANGE}, {"yellow", YELLOW}, {"green", GREEN}, {"blue", BLUE}, {"violet", VIOLET}, {"gray", GRAY}, {"white", WHITE} }; //resistor multiplier values int multiplier[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000}; char answer[3][10]; // User input int colour_val[3]; //stores the band value

#define nItems (sizeof item_list/sizeof *item_list) //function prototyps int srchItems (char *ccode); //a search for index values char *strcpy2lower (char *dest, char *src); //converts to lower case int scmp (char *a, char *b); //simple string comarison char *sepnumber (char *s, long val); int invalid (char answer[3][10]); //main function int main(int argc, char const *argv[]) { int i; // counter char status = 'Y';// Keeps the program running when user inputs 'Y' long resistor_value = 0; //Total resistance int r, err, mult; //holds the significant figure, Error, Multiplier char resistor_value_string[20] = "";//formatted output while (status == 'Y') //The program runs under this { //print the question to the user printf("Enter the colours of the resistor's three bands, beginning with the band nearest to the end. Type the coloues in lowercase letters only, NO CAPS. "); for (i = 0; i < MINC; ++i) { printf("Band %d => ", i + 1); // print headers for each band scanf(" %s", &answer[i]); // get the user input } for (i = 0; i < MINC - 1; ++i) //converts colours into index { if ((r = srchItems (answer[i])) > -1) { // from significant figure resistor_value = (resistor_value * 10) + r; } else { invalid(answer); err = 2;

} } if (err > 1) { printf("Do you want to decode anothe resistor (Y/N)? "); scanf (" %c", &status); if (status == 'Y'); else break; } else { mult = srchItems (answer[i]); // get multiplier index resistor_value *= multiplier[mult]; // Calculate final value

sepnumber (resistor_value_string, resistor_value); printf("Resistor value: "); /*for (int i = 0; i < (strlen(resistor_value_string) ); ++i) { printf("%c", resistor_value_string[i]); } */ puts (resistor_value_string); //printf(" -ohm "); //memset (resistor_value_string, 0, 50); printf("Do you want to decode anothe resistor (Y/N)? "); scanf (" %c", &status); if (status == 'Y'); else break; /*Debug for (int i = 0; i < MINC; ++i) { printf("item_list[%d] = %s ", i, answer[i]); } printf("Total resistance = %ld ", resistor_value); //end of debug */ } } return 0; }

int srchItems (char *ccode) { int i; char lccode [MAXCC] = ""; strcpy2lower (lccode, ccode); // converts everything to lower case for (int i = 0; i < (int)nItems; ++i) if (*lccode == *(item_list[i].name)) if (!scmp(item_list[i].name, lccode)) return i; return -1; }

char *strcpy2lower (char *dest, char *src) { if (!src || !dest) return NULL; char *d = dest; for (; *src; src++, d++) if ('A' <= *src && *src <= 'Z') *d = *src | (1 << 5); else *d = *src; *d = 0; return dest; }

int scmp (char *a, char *b) { if (!a && !b) return 0; if ( a && !b) return 1; if (!a && b) return -1;

for (; *a && *b && *a == *b; a++, b++) {}

return *a - *b; }

/** separate long value every 3rd char into 's' */ char *sepnumber (char *s, long val) { char numstr[3 * MAXCC] = ""; char *p = numstr; size_t idx = 0, len = 0;

sprintf (numstr, "%ld", val); for (; *p; p++) {} len = p - numstr; p = s + 3 * MAXCC - 2;

while (len--) { if (idx++ == 3) { idx = 1; *p-- = ' '; } *p = numstr[len]; if (len) p--; } for (idx = 0; *p; p++, idx++) s[idx] = *p; /* copy to s */ s[idx] = *p; /* nul-terminate */ return s; }

int invalid (char answer[3][10]) { int r, counter = 0, incorrect[3], i;

for (i = 0; i < MINC; ++i) { if ((r = srchItems (answer[i])) == -1) { incorrect[i] = r; counter++; } } if (counter == 1) { printf("%s","Invalid colour: " ); printf("%s ", answer[i]); printf(" "); } i = 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!