十进制到二进制转换程序"中止陷阱:6"错误

时间:2016-02-06 05:56:39

标签: c runtime-error

我在C中编写了一个小程序来帮助自己学习从十进制到二进制的转换。当我运行它时,它打印出一个随机数供我转换,并等待我输入转换后的数字。输入十进制数字后按回车键,它将发送我的" CONGRATS!"消息,如果我做对了,但也会有错误:

  

中止陷阱:6

在输出中,即使程序大部分都一直运行到最后。我知道#include <stdlib.h> #include <time.h> #include <stdio.h> #define MAX_NUMBER 256 // random number will fall between 0 and 255 #define BI_NUM_SIZE 8 // represents the 8 spaces needed for each number /* This function does the exponentiation operation to ints and returns an int */ int exponent (int base, int power) { int answer = base, i; if (power < 0) { printf ("ERROR: no negative numbers as the power " "in the exponent function!\n"); return 0; } if (power == 0) { return 1; } else { for (i = 1; i < power; i++) { answer *= base; } return answer; } } int main () { int number_to_convert, i, j, k; char possible_answer[BI_NUM_SIZE]; char answer[BI_NUM_SIZE]; char answer_check; srand (time (NULL)); number_to_convert = rand () % MAX_NUMBER; // this for loop solves the decimal to binary conversion // and stores the character array answer in 'answer'; // as i decrements for the conversion math j will from 0 to place // characters in the answer array j = 0; // allows for indexing through answer[] // starts as number_to_convert but gets widled down int temp_number = number_to_convert; for (i = BI_NUM_SIZE; i > 0; i--) { // as part of the if (temp_number >= exponent (2, i - 1)) { answer[j] = '1'; temp_number -= exponent (2, i - 1); } else { answer[j] = '0'; } j++; } printf ("Convert %3d to binary:\n", number_to_convert); printf ("type your answer and press enter:\n"); scanf ("%s", &possible_answer); answer_check = 0; for (i = 0; i < BI_NUM_SIZE; i++) { if (answer[i] == possible_answer[i]) { answer_check++; } } if (answer_check == BI_NUM_SIZE) { printf ("CONGRATS! you got it right!\n"); } else { printf ("sorry the answer is "); for (i = 0; i < BI_NUM_SIZE; i++) { printf ("%c", answer[i]); } } return 0; } 与阵列有关,但除此之外我不确定。有任何想法吗?哦,以下是我的计划。

table td {
  height: 25px;
  width: 25px;
  border-width: 1px;
  border-style: solid;
  border-color: grey;
}
td.X {
  content: url('http://files.softicons.com/download/game-icons/super-mario-icons-by-sandro-pereira/png/64/Luma%20-%20Red.png');
}
td.Y {
  content: url('http://files.softicons.com/download/game-icons/super-mario-icons-by-sandro-pereira/png/32/Mushroom%20-%20Mini.png');
}

1 个答案:

答案 0 :(得分:1)

使用scanf格式说明符 %s阅读信息时,scanf将读取字符,直到第一个空白为止遇到,将结果存储为给定的相应指针参数中的 nul-terminated 字符串。如果您打算将用户输入的8个字符读入possible_answer,那么scanf调用的正确形式(具有适当的宽度限制)为:

scanf ("%8s", possible_answer);

注意:'&'前面没有possible_answer,因为它已经是指针。您还应该检查返回确保1成功转换。)

对于 nul-terminatedating 字符,您必须为要读取的字符数+1提供足够的空间。如果您打算将8个字符读入possible_answer,则必须为9个字符提供存储空间。例如:

#define BI_NUM_SIZE 8+1

另请注意,这将需要调整依赖于BI_NUM_SIZE以下的所有循环和测试边界。 e.g:

for (i = BI_NUM_SIZE - 1; i > 0; i--) {
...
if (answer_check == BI_NUM_SIZE - 1) {

包含每项更改的相关代码为:

    for (i = BI_NUM_SIZE - 1; i > 0; i--) {
        // as part of the
        if (temp_number >= exponent (2, i - 1)) {
            answer[j] = '1';
            temp_number -= exponent (2, i - 1);
        } else {
            answer[j] = '0';
        }
        j++;
    }
    printf ("Convert %3d to binary\n", number_to_convert);
    printf ("answer and press enter: ");
    scanf ("%8s", possible_answer);

    answer_check = 0;
    for (i = 0; i < BI_NUM_SIZE - 1; i++) {
        if (answer[i] == possible_answer[i]) {
            answer_check++;
        }
    }

    if (answer_check == BI_NUM_SIZE - 1) {
        printf ("\nCONGRATS! you got it right!\n\n");
    } else {
        printf ("sorry the answer is   : ");
        for (i = 0; i < BI_NUM_SIZE - 1; i++) {
            printf ("%c", answer[i]);
        }
        putchar ('\n');
    }
    return 0;

进行这些更改后,您的代码应该可以正常运行。 注意:在编写代码时,您必须在答案中提供所有前导0's。 e.g:

<强>输出

$ ./bin/bincnv
Convert 117 to binary
answer and press enter: 00000001
sorry the answer is   : 01110101

$ ./bin/bincnv
Convert  71 to binary
answer and press enter: 01000111

CONGRATS! you got it right!