十进制转二进制程序"abort trap: 6"错误

Decimal to binary conversion program "abort trap: 6" error

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

Abort trap: 6

在输出中,即使程序大部分 运行 一直到最后都很好。我知道 Abort trap: 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;
}

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

scanf ("%8s", possible_answer);

(注意: 你不要在 possible_answer 前面加上一个 '&' 因为它已经是一个 指针 。您还应该检查 return 以确保 1 次成功转换。)

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

#define BI_NUM_SIZE 8+1

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

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。例如:

输出

$ ./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!