strcmp将无法在if语句中正确评估

时间:2015-11-24 16:10:11

标签: c fgets strcmp

#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 
}

字符串正确存储;问题在于,在功能中,在比较中没有找到正确的值,以便用算法找到阻力。如果用户输入'red','red','red',则值n,n1和exp将等于-1,-1,0。可能导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分:7)

这里的问题fgets()扫描并将尾随换行符也存储到输入缓冲区中。

在发送输入以进行比较之前,您需要删除该换行符。否则,您的字符串比较很可能会失败。

一个非常简单的解决方案可以是,检查输入的字符串长度,然后检查新行(\n)的最后一个索引值,如果找到,用null替换它({{1然后,将输入传递给比较函数。

答案 1 :(得分:0)

fgets在其读取的字符串中包含换行符。您要么必须删除它,要么使用不包含换行符的scanf之类的函数。 (如果使用scanf,请记住使用例如%7s说明符来防止缓冲区溢出。