I am writing a program that will calculate resistor values based on colour input from user. The function that is giving me trouble is intended to get a colour in the form of a string, and return the matching integer value.
However, despite various changes, it has only been returning the value from the else
statement, 100, which is just a message to main()
that the string did not match any of the colours.
The unfinished code is below:
#include <stdio.h>
#include <math.h>
int values123(char c[10]);
void main(void)
{
int bands = 0;
char band1[10];
char band2[10];
char band3[10];
char band4[10];
char band5[10];
printf("Number of colour bands: ");
scanf_s("%i", &bands);
printf("\nBand 1: ");
scanf_s("%s", band1);
if (values123(band1) == 100)
{
printf("Colour is invalid!");
}
fflush(stdin);
printf("\nBand 2: ");
scanf_s("%s", band2);
fflush(stdin);
printf("\nBand 3: ");
scanf_s("%s", band3);
fflush(stdin);
printf("\nBand 4: ");
scanf_s("%s", band4);
fflush(stdin);
if (bands == 5)
{
printf("\nBand 5: ");
scanf_s("%s", band5);
fflush(stdin);
}
getch();
}
int values123(char c[10])
{
if (strcmp(c, "black") == 0)
return (0);
else if (strcmp(c, "brown") == 0)
return (1);
else if (strcmp(c, "red") == 0)
return (2);
else if (strcmp(c, "orange") == 0)
return (3);
else if (strcmp(c, "yellow") == 0)
return (4);
else if (strcmp(c, "green") == 0)
return (5);
else if (strcmp(c, "blue") == 0)
return (6);
else if (strcmp(c, "violet") == 0)
return (7);
else if (strcmp(c, "grey") == 0)
return (8);
else if (strcmp(c, "white") == 0)
return (9);
else
return (100);
}
Please feel free to inform me of any mistakes I am making, whether they are related to the issue or not, as I am sure I am making a ton!
By the way, this is not a homework question (as much as it looks like one), I am an Electronics Engineering Technology student and was in the mood to practice C by making a program related to what I am studying :)
Thanks!