当用户输入double值时给出错误

时间:2012-10-21 13:20:00

标签: c

我写了一个决定三角形类型的程序。当用户输入字符串输入时,它会给出错误。我想为双输入提供错误。为了做到这一点,我尝试了这个

else if (s1[i] == '.') {
                found_double = 1;
                break;

但程序也将.识别为字符串。我怎么解决这个问题?完整代码如下。

/*
 * HW3-3.c
 *
 *  Created on: Oct 21, 2012
 *      Author: mert
 */


 #include <string.h>
 #include <stdio.h>

 void checkTriangle(char *s1,char *s2,char *s3)
  {
    int i;
    int found_double = 0;
    int found_letter = 0;
    int len = strlen(s1);
    int len2 = strlen(s2);
    int len3 = strlen(s3);

    for( i = 0; i < len; i++)
    {
        if(s1[i] < '0' || s1[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        } else if (s1[i] == '.') {
            found_double = 1;
            break;
        }
    }


    for( i = 0; i < len2; i++)
    {
        if(s2[i] < '0' || s2[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        } else if (s2[i] == '.') {
            found_double = 1;
            break;
        }
    }


    for( i = 0; i < len3; i++)
    {
        if(s3[i] < '0' || s3[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        } else if (s3[i] == '.') {
            found_double = 1;
            break;
        }
    }

    if(found_letter) // value 0 means false, any other value means true
        printf("Please enter an integer instead of string.");
    else if (found_double)
        printf("Please enter an integer instead of double.");
    else
    {
            int side1 = atoi(s1);
            int side2 = atoi(s2);
            int side3 = atoi(s3);

            if ((side1 + side2 > side3 && side1 +  side3 > side2 && side2 + side3 > side1) && (side1 > 0 && side2 > 0 && side3 > 0))
                 {
                // Deciding type of triangle according to given input.
                  if (side1 == side2 && side2 == side3)
                      printf("EQUILATERAL TRIANGLE");
                  else if (side1 == side2 || side2 == side3 || side1 == side3)
                      printf("ISOSCELES TRIANGLE\n");
                  else
                      printf("SCALENE TRIANGLE \n");
                }
                 else
                     printf("\nTriangle could not be formed.");
    }
  }


  int main(void)
  {

      char s[32], s2[32], s3[32];

      printf("Please enter sides of triangle");
      printf("\nPlease enter side 1:");
      gets(s);
      printf("Please enter side 2:");
      gets(s2);
      printf("Please enter side 3:");
      gets(s3);

      checkTriangle(s,s2,s3);
  }

2 个答案:

答案 0 :(得分:0)

  1. s1是一个char *所以它应该是'。'将(在你的话中)一个字符串。
  2. '。'的价值小于'0'(或者可能大于'9'),所以如果
  3. ,你将输入第一个if并跳过else

答案 1 :(得分:0)

您可以更改IF的顺序。

 if (s1[i] == '.')
 {
       //code here
 }
 else if(s1[i] < '0' || s1[i] > '9')
 {
     //code here
 }

因为'。'的ascii代码定义为不在区间['0','9']中,所以它总是会被捕获在第一个IF中。

您可以尝试以下代码来查看其ascii值:

  printf("Ascii code for char '.' is %d\n", '.');
  printf("Ascii code for char '0' is %d\n", '0'); 
  printf("Ascii code for char '9' is %d\n", '9'); 
相关问题