if语句的strcmp将无法正确评估 [英] strcmp will not correctly evaluate in if statements

查看:155
本文介绍了if语句的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 
}

中的字符串被妥善保存;的问题是,函数内的正确的值不被所述比较中发现的,以便找到与算法的阻力这一事实。如果用户输入红,红,红,n的数值中,n1和EXP将等于-1,-1,0。可能是什么原因?

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?

推荐答案

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

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

您需要发送的输入进行比较之前摆脱了换行符。否则,你的字符串比较是pretty太多可能会失败。

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

有一个非常简单的解决方案可以像,检查输入的字符串长度,然后对新行的最后一个索引值检查( \\ n ),如果你发现了即,替换成空( \\ 0 ),然后,通过对输入到比较功能(S)。

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).

这篇关于if语句的strcmp将无法正确评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆