By Trey


2015-11-24 16:10:10 8 Comments

#include <stdio.h>
#include <math.h>
#include <string.h>
#define size 7

int computeN(char s1[])
    {
        int n=-1;

        if(strcmp(s1, "black") == 0)
        {
            n = 0;
        }
        else if (strcmp(s1, "brown") == 0)
        {
            n = 10;
        }
        else if (strcmp(s1, "red") == 0)
        {
            n = 20;
        }
        else if (strcmp(s1, "orange") == 0)
        {
            n = 30;
        }   
        else if (strcmp(s1, "yellow") == 0)
        {
            n = 40;
        }
        else if (strcmp(s1, "green") == 0)
        {
            n = 50;
        }
        else if (strcmp(s1, "blue") == 0)
        {
            n = 60;
        }
        else if (strcmp(s1, "violet") == 0)
        {
            n = 70;
        }
        else if (strcmp(s1, "grey") == 0)
        {
            n = 80;
        }
        else if (strcmp(s1, "white") == 0)
        {
            n = 90;
        }
        printf("%d\n", n);
        return n;
    }

int computeN2(char s2[])
    {
        int n1=-1;  

        if(strcmp(s2, "black") == 0)
        {
            n1 = 0;
        }
        else if (strcmp(s2, "brown") == 0)
        {
            n1 = 1;
        }
        else if (strcmp(s2, "red") == 0)
        {
            n1 = 2;
        }
        else if (strcmp(s2, "orange") == 0)
        {
            n1= 3;
        }   
        else if (strcmp(s2, "yellow") == 0)
        {
            n1 = 4;
        }
        else if (strcmp(s2, "green") == 0)
        {
            n1 = 5;
        }
        else if (strcmp(s2, "blue") == 0)
        {
            n1 = 6;
        }
        else if (strcmp(s2, "violet") == 0)
        {
            n1 = 7;
        }
        else if (strcmp(s2, "grey") == 0)
        {
            n1 = 8;
        }
        else if (strcmp(s2, "white") == 0)
        {
            n1 = 9;
        }
        printf("%d\n", n1);
        return n1;
    }

int computeExponent(char s3[])
{
        int  exp=0;

        if(strcmp(s3, "black") == 0)
        {
            exp = 1;
        }
        else if (strcmp(s3, "brown") == 0)
        {
            exp = 10;
        }
        else if (strcmp(s3, "red") == 0)
        {
            exp = 100;
        }
        else if (strcmp(s3, "orange") == 0)
        {
            exp = 1000;
        }   
        else if (strcmp(s3, "yellow") == 0)
        {
            exp = 10000;
        }
        else if (strcmp(s3, "green") == 0)
        {
            exp = 100000;
        }
        else if (strcmp(s3, "blue") == 0)
        {
            exp = 1000000;
        }
        else if (strcmp(s3, "violet") == 0)
        {
            exp = 10000000;
        }
        else if (strcmp(s3, "gray") == 0)
        {
            exp = 100000000;
        }
        else if (strcmp(s3, "white") == 0)
        {
            exp = 1000000000;
        }
        printf("%d\n", exp);
        return exp;
    }

int computeResistance(int x, int y, int z)
{
    int omega = ((x+y) * z);
    return omega;
}


int main(void)
{
    char color_codes[10][7] = {"black","brown","red","orange","yellow","green","blue","violet","gray","white"};
    char s1[7], s2[7], s3[7];
    int  n, n1, choice;

    printf("Enter the colors of the resistor's three bands, beginning with\n");
    printf("the band nearest the end. Type the colors in lowercase letters\n");
    printf("only, NO CAPS\n");

    printf("Band 1 =>\n");              //prints prompts for bands 
    fgets(s1, size, stdin);             //stores band 1 in s1

    printf("Band 2 => \n");             //prints prompt 
    fgets(s2, size, stdin);             //stores band 2 in s2

    printf("Band 3 => \n");             //prints prompt 
    fgets(s3, size, stdin);             //stores band 3 in s3


    printf("Resistance value: %d\n", computeResistance(computeN(s1), computeN2(s2), computeExponent(s3)));  //computes resistance 

    return (0);                                             //make the exit 
}

The strings are being stored properly; the issue is the fact that within the functions the correct value is not being found within the comparison in order to find the resistance with the algorithm. If the user enters 'red', 'red', 'red', the values n,n1, and exp will be equal to -1,-1,0. What could be causing this?

2 comments

@Sourav Ghosh 2015-11-24 16:18:03

The problem here is, fgets() scans and stores the trailing newline, too, into the input buffer.

You need to get rid of that newline before sending the input for comparison. Otherwise, your string comparison is pretty much likely to fail.

A very simple solution can be like, check the string length of the input, then check on the last index value against the newline (\n), if you found that, replace that with null (\0) and then, pass on the input to the comparison function(s).

@nneonneo 2015-11-24 16:21:07

fgets includes the newline character in the string it reads. You'll either have to remove it or use a function like scanf which does not include the newline. (If you use scanf, remember to use e.g. %7s specifiers to prevent buffer overflows).

Related Questions

Sponsored Content

9 Answered Questions

5 Answered Questions

[SOLVED] How to pass an array of struct using pointer in c/c++?

2 Answered Questions

[SOLVED] Passing of values from text file to array

  • 2016-12-12 13:55:51
  • Jefferson Ponce
  • 80 View
  • 0 Score
  • 2 Answer
  • Tags:   c

1 Answered Questions

[SOLVED] Problems with a program that decodes resistance by color in C

  • 2016-12-05 02:12:16
  • Jefferson Ponce
  • 80 View
  • 0 Score
  • 1 Answer
  • Tags:   c decoder

1 Answered Questions

[SOLVED] How can I set the brown color using ANSI escape sequence?

  • 2015-12-10 12:04:06
  • Maxim Gusev
  • 625 View
  • 0 Score
  • 1 Answer
  • Tags:   c linux sequence ansi

1 Answered Questions

[SOLVED] Printing an array of pointers using double pointer

  • 2013-11-24 06:01:47
  • mosdellg
  • 2064 View
  • 0 Score
  • 1 Answer
  • Tags:   c arrays pointers

Sponsored Content