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


i wrote little program in c myself learn converting decimal binary. when run it, prints out random number me convert , waits me enter in converted number. after entered decimal number , press enter send "congrats!" message if got right have error:

abort trap: 6

in output though program runs fine way through end. know abort trap: 6 has arrays i'm not sure besides that. ideas? oh , below program.

#include <stdlib.h> #include <time.h> #include <stdio.h>  #define max_number 256  // random number fall between 0 , 255 #define bi_num_size 8   // represents 8 spaces needed each number  /* function exponentiation operation    ints , returns int */ int exponent (int base, int power) {     int answer = base, i;     if (power < 0) {         printf ("error: no negative numbers power "                 "in exponent function!\n");         return 0;     }     if (power == 0) {         return 1;     } else {         (i = 1; < 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;      // loop solves decimal binary conversion     // , stores character array answer in 'answer';     // decrements conversion math j 0 place     // characters in answer array     j = 0;  // allows indexing through answer[]      // starts number_to_convert gets widled down     int temp_number = number_to_convert;      (i = bi_num_size; > 0; i--) {         // part of         if (temp_number >= exponent (2, - 1)) {             answer[j] = '1';             temp_number -= exponent (2, - 1);         } else {             answer[j] = '0';         }         j++;     }     printf ("convert %3d binary:\n", number_to_convert);     printf ("type answer , press enter:\n");     scanf ("%s", &possible_answer);      answer_check = 0;     (i = 0; < bi_num_size; i++) {         if (answer[i] == possible_answer[i]) {             answer_check++;         }     }      if (answer_check == bi_num_size) {         printf ("congrats! got right!\n");     } else {         printf ("sorry answer ");         (i = 0; < bi_num_size; i++) {             printf ("%c", answer[i]);         }     }     return 0; } 

when reading information using scanf , format-specifier %s, scanf read characters until first whitepace encountered, storing result nul-terminated string in corresponding pointer argument given. if intend read 8-characters entered user possible_answer, correct form of scanf call, appropriate width limitation, is:

scanf ("%8s", possible_answer); 

(note: not include '&' in front of possible_answer because pointer. should check return insure 1 successful conversion made.)

you must provide adequate space number of characters read +1 nul-terminating character. if intend read 8-characters possible_answer, must provide storage 9-charactes. example:

#define bi_num_size 8+1 

also note, require adjusting of loop , test bounds rely on bi_num_size below. e.g.:

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

the relevant code each of changes included is:

    (i = bi_num_size - 1; > 0; i--) {         // part of         if (temp_number >= exponent (2, - 1)) {             answer[j] = '1';             temp_number -= exponent (2, - 1);         } else {             answer[j] = '0';         }         j++;     }     printf ("convert %3d binary\n", number_to_convert);     printf ("answer , press enter: ");     scanf ("%8s", possible_answer);      answer_check = 0;     (i = 0; < bi_num_size - 1; i++) {         if (answer[i] == possible_answer[i]) {             answer_check++;         }     }      if (answer_check == bi_num_size - 1) {         printf ("\ncongrats! got right!\n\n");     } else {         printf ("sorry answer   : ");         (i = 0; < bi_num_size - 1; i++) {             printf ("%c", answer[i]);         }         putchar ('\n');     }     return 0; 

after make changes, code should work without issue. note: must provide leading 0's in answer code written. e.g.:

output

$ ./bin/bincnv convert 117 binary answer , press enter: 00000001 sorry answer   : 01110101  $ ./bin/bincnv convert  71 binary answer , press enter: 01000111  congrats! got right! 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

dataset - MPAndroidchart returning no chart Data available -

post - imageshack API cURL -