I've spent a little over a week trying to get a handle on strings. I've gone to my teacher (the help she was able to give me she did by looking at the answer key because the wording and instructions in the assignment confused her as well.
I've got a pretty good grasp on C programming as a whole, just took my final and got a 100 on it, but writing strings is an area that I'm struggling with a lot. I've gone to youtube, google, the book, you name it. This is the only thing keeping me from getting the dean's list and I wouldn't seek help in this manner if I hadn't exhausted all other methods.
My code is supposed to take user inputs for the colors of a resistor, make them user string all lower case, replace grey with gray and purple with violet, and then finally display the numerical equivalent of the color band. Below is what I have this far. I have an idea of what I need to do, but everything I've tried thus far hasn't produced the desired results
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
/*color codes*/
char COLOR_CODES[10][7] = {"black", "brown","red","orange","yellow","green","blue","violet","gray","white"};
int readColor(int BandNumber) {
int colorNumber = -1;
int i;
/* Decalre a string to hold the color name typed in at the keyboard. */
do {
/* Prompt to read color name for the indicated band.*/
printf(" " );
/*read a color band name as a string.*/
_strlwr();
for (i = 0; (i < 10) && (colorNumber == -1); ++i) {
if (strcmp(colorBandName, COLOR_CODES[i]) ==0) {
colorNumber = i;
}
}
if (colorNumber == -1);
printf("The color"%s"was not recognized.\n");
printf("Please re-enter.\n", colorBandName);
} while (colorNumber == -1);
return (colorNumber);
}
void outresistance(double Resistance) {
printf("resistance is:");
If (Resistance <= 999.95){
} else if()
}else if
}else
Printf("%.1fMEga=ohms.\n",resistance/100000.00);
}
}
int Main(void){
int band1,band2,band3;
double resitance;
char yesNoAnwers[20];
char colorBandName[20];
do{
printf("enter the three color bands of a resistor,");
printf("one at a time as prompted.\n");
band1 = readColor(1);
band2 = readColor(2);
band3 = readColor (3);
outputResistance(resistance);
printf("[answers Y or N]");
scanf("s%",colorBandName);
_strlwr( );
}while (/*compare answer for yes*/);
return(0);
}