1

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!

wildplasser
  • 43,142
  • 8
  • 66
  • 109
Ctrl S
  • 1,053
  • 12
  • 31
  • 1
    You can't compare strings using `==` - you need to take a look at some of the C string comparison functions. – PeterJ Feb 05 '14 at 05:44
  • `scanf_s("%s", &band5);` should be `scanf_s("%s", band5);` – Grijesh Chauhan Feb 05 '14 at 05:44
  • Why no ampersand? What does it do exactly? Sometimes my scanf's don't work without it :/ And thx PeterJ, I'll look into that! Thanks for the extremely quick responses everyone :o – Ctrl S Feb 05 '14 at 05:47
  • Use strcmp() I guess? Seems like that should do it... – Ctrl S Feb 05 '14 at 05:50
  • Unrelated, but problematic: `fflush(stdin)` is not defined behavior per the C standard. The passed stream must be either an output stream or an update stream where the most-recent action was a write. There are ways to "flush" stdin, but this isn't one of them. – WhozCraig Feb 05 '14 at 06:05
  • Yeah I tried removing the & and it crashes when it gets to the scanf. :s – Ctrl S Feb 05 '14 at 06:06
  • @WhozCraig Yeah I just used the first buffer flush statement I could find since without it, it skips through all the scanf's without taking input. – Ctrl S Feb 05 '14 at 06:12
  • This is no longer a duplicate since the issue still occurs even when strcmp() is used. – Ctrl S Feb 05 '14 at 12:31
  • Please don't update the question by applying changes from the answers. I will make the answers look like nonsense (instead of the question) – wildplasser Feb 05 '14 at 13:15
  • I understand, but the webpage suggested editing the question to explain what has changed. I have posted a new question altogether now though. – Ctrl S Feb 05 '14 at 13:20

1 Answers1

1

If you use == to compare two strings it's comparing the address of the two strings is equal or not which is definitely unequal. So try the following

if(strcmp(c, "black") == 0)
{
   return 0;
}

And while using scanf on strings(array of characters ) you don't need to explicitly use & since in C arrays by default pass address.

EDIT : use else if ladder since it's searching on similar set.

KARTHIK BHAT
  • 1,410
  • 13
  • 23
  • I actually just finished trying that before seeing this and expected it to work, however it seems to still be returning 100 from else. Glad that I'm learning and correcting these basic mistakes though! – Ctrl S Feb 05 '14 at 06:02
  • what was input ?? you have considered alphabet lower and upper case's also rite ? – KARTHIK BHAT Feb 05 '14 at 06:04
  • Also when I remove the &, the program crashes at the first scanf it encounters... – Ctrl S Feb 05 '14 at 06:05
  • only for strings you have to remove & not for the number bands should have & – KARTHIK BHAT Feb 05 '14 at 06:08
  • I have been using 'red' as my input every time (all lowercase). I haven't bothered implementing uppercase as well; was going to do that once I got lowercase working so I could just copy the format. – Ctrl S Feb 05 '14 at 06:09
  • have you used (strcmp(x,y) == 0)??? if not you will always get to else part. since on success it returns 0. – KARTHIK BHAT Feb 05 '14 at 06:13
  • Yep, using strcmp() now. Still giving me incorrect value, but at least I'm comparing strings properly now, haha :P @Karthik else if: Ah thanks, yeah I wasnt sure if C used else if >_> &: Changed that now - thanks! – Ctrl S Feb 05 '14 at 06:20
  • best would be use printf in function so you know what exactly is happening (debugging using printf) – KARTHIK BHAT Feb 05 '14 at 06:22
  • Yeah I'll try that when I get a chance, thanks! – Ctrl S Feb 05 '14 at 06:28