2016-03-08 67 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

相關問題