2016-03-08 66 views
1

我正在开发一个类项目,我想要做一些额外的工作并对我的数据进行验证。这个问题似乎在num1 = num1Input(和num2 = num2Input)在那里得到的位置(我假设)发生的,而不是实际的输入值C输出可变位置(指针)而不是实际值

int main(void) { 
    //variables 
    char num1input[10]; 
    char num2input[10]; 

    int length, i; 
    int num1 = 0; 
    int num2 = 0; 
    int countErrors1 = 0; 
    int countErrors2 = 0; 

    bool correct1 = false; 
    bool correct2 = false; 

    //--end of variable declarations--// 

    do { 
     printf("Please enter a number: "); 
     scanf("%s", num1input); 
     length = strlen(num1input); 
     for (i = 0; i < length; i++) { 
      if (!isdigit(num1input[i])) { 
       countErrors1++; 
      } 
     } 
     if (countErrors1 > 0) { 
      printf("Input is not a number \n"); 
     } else { 
      correct1 = true; 
     } 
    } while (correct1 == false); 
    num1 = num1input; 

    do { 
     printf("Please enter second number: "); 
     scanf("%s", num2input); 
     length = strlen(num2input); 
     for (i = 0; i < length; i++) { 
      if (!isdigit(num2input[i])) { 
       countErrors2++; 
      } 
     } 
     if (countErrors2 > 0) { 
      printf("Input is not a number \n"); 
     } else { 
      correct2 = true; 
     } 
    } while (correct2 == false); 
    num2 = (int)num2input; 

    printf("%d %d \n", num1, num2); 

    int addition = num1 + num2; 
    int substraction = num1 - num2; 
    int multiplication = num1 * num2; 
    float division = num1/num2; 

    printf("Addition: %d Subtraction: %d Multiplication: %d Division: %.1e", addition, substraction, multiplication, division); 

    getch(); 
} 
+2

C不做投十进制到二进制的转换。您正在寻找['strtol'](http://linux.die.net/man/3/strtol)。 – zwol

+0

我只希望num1Input中的值存储在num1中。我能做什么? –

+2

你可以像我说的那样使用'strtol'。 (另外,忘记了你曾经听说过'scanf',它被指定为坏了;在C中用于用户输入的正确函数是'getline',否则'fgets')。 – zwol

回答

1

不能字符串转换为数字同为num1 = num1input;这样的演员。你需要从<stdlib.h>调用库函数:

#include <stdlib.h> 

... 

num1 = atoi(num1input); 

atoi忽略了解析错误。为了确保检测到溢出,你可以使用strtol()如下:

#include <errno.h> 
#include <limits.h> 
#include <stdlib.h> 

... 

errno = 0; 
char *endp; 
long lval = strtol(num1input, &endp, 10); 
if (endp == num1input || errno != 0 || lval < INT_MIN || lval > INT_MAX) { 
    /* parse error detected: 
    * you could print an error message. 
    */ 
    if (lval < INT_MIN) lval = INT_MIN; /* clamp lval as an int value. */ 
    if (lval > INT_MAX) lval = INT_MAX; 
} 
num1 = lval; 

或者,如果你想认识十六进制语法如0x10

num1 = strtol(num1input, NULL, 0); 

这同样适用于num2input

请注意,如果char已签名且num1input[i]具有负值,则isdigit(num1input[i])可能不正确。你应该写:

isdigit((unsigned char)num1input[i]) 

另外请注意,float division = num1/num2;将计算整数除法并将结果转换为float。如果你想在浮点除法,你应该写:

float division = (float)num1/num2; 

最后注意到,它建议使用double,而不是float具有更高的精度。

这里是一个纠正和简化版:

#include <errno.h> 
#include <limits.h> 
#include <stdlib.h> 
#include <stdio.h> 

/* simple implementation of strtoi(), inspired by elegant code from chux */ 
int strtoi(const char *s, char **endptr, int base) { 
    long y = strtol(s, endptr, base); 
#if INT_MAX != LONG_MAX 
    if (y > INT_MAX) { 
     errno = ERANGE; 
     return INT_MAX; 
    } 
#endif 
#if INT_MIN != LONG_MIN 
    if (y < INT_MIN) { 
     errno = ERANGE; 
     return INT_MIN; 
    } 
#endif 
    return (int)y; 
} 

int main(void) { 
    char num1input[20]; 
    char num2input[20]; 
    char *endp; 
    int num1, num2; 

    for (;;) { 
     printf("Please enter a number: "); 
     if (scanf("%19s", num1input) != 1) 
      return 1; 
     errno = 0; 
     num1 = strtoi(num1input, &endp, 10); 
     if (errno == 0 && *endp == '\0') 
      break; 
     printf("Input is not a number\n"); 
    } 

    for (;;) { 
     printf("Please enter a second number: "); 
     if (scanf("%19s", num2input) != 1) 
      return 1; 
     errno = 0; 
     num2 = strtoi(num2input, &endp, 10); 
     if (errno == 0 && *endp == '\0') 
      break; 
     printf("Input is not a number\n"); 
    } 

    printf("%d %d\n", num1, num2); 

    int addition = num1 + num2; 
    int subtraction = num1 - num2; 
    int multiplication = num1 * num2; 
    double division = (double)num1/num2; 

    printf("Addition: %d Subtraction: %d Multiplication: %d Division: %g\n", 
      addition, subtraction, multiplication, division); 
    getch(); 
} 
+1

Downvoted为暗示'atoi',它静静地忽略错误。只有'strtol'家族应该被推荐用于这个目的。 – zwol

+0

确实'atoi'忽略错误,但是'strtol()'解析并返回一个'long',这将需要进一步的测试来检测溢出。为什么委员会没有为'int'值规范'strtoi()'函数是不一致的。我会更新答案。 – chqrlie

+0

是的,缺乏'strtoi'是有点疣。 – zwol

相关问题