Search code examples
carraysmathcharbus

character arithmetic in c;Bus error


My program is supposed to take in 2 strings and do arithmetic.

Example:

input: abc+aab

output: abc + aab => bce

The program takes the user input string and loads the 2 parts in to a multidimensional array and a char variable for the arithmetic symbol. It is supposed to convert the characters to their numerical equivalence(ASCII) to do the arithmetic. Then it is supposed to output the values as characters again. When the numerical value exceeds 26 it takes the character from the first part of the string and outputs its capitalized form.

Example:

input: d+y

output: d + y => D

This seems simple enough but I am more experienced with java and I think there is a loss of translation in my code that is causing the runtime error: Bus error on line 44: if (a[2][i] >= 27){

For reference, when I compile I type: gcc -g -o prog06 prog06.c -lm

Then to run with gdb I type: gdb prog06

Code so far:

/* My Name
   C & Unix
   prog06
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main() {
  int i, j, k, a[3][9];
  char input[19], b, c[10];
  system("clear");

  printf("This is a string arithmatic program of SOS form.\n");

input:
  printf("Input: ");
  scanf("%s", input);
  for (i = 0; i < strlen(input); i++){
    if (input[i] != '+' || input[i] != '-' || input[i] != '/' || input[i] != '*'){
      a[j][k] == input[i] - '`';
      k++;              //Converts input to a multidimensional string array
      continue;             //a[][] + char b for arithmatic character.
    }
    if (input[i] == '+' || input[i] == '-' || input[i] == '/' || input[i] == '*'){
      a[j][k+1] = '\0';
      b = input[i];
      j++;
      k = 0;
      continue;
    }

  }
  if (b == '+') goto add;
  if (b == '-') goto sub;
  if (b == '/') goto div;
  if (b == '*') goto mul;

add:
  i = 0;
  do {
    a[2][i] = a[0][i] + a[1][i];
    if (a[2][i] >= 27){
      a[2][i] = a[0][i] + 64;
    }
    i++;
  } while (a[0][i] != '\0' || a[1][i] != '\0'); j = i;
  printf("\n%s + %s => ", a[0], a[1]);
  goto output;

sub:

div:

mul:

output:
  for (i = 0; i < j; i++){
    c[i] = a[2][i];
  }
  printf("%s", c);
}

Solution

  • sample to fix

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void) {
        int i, j, k, a[3][9];
        char input[19], b, c[10];
        system("clear");
    
        printf("This is a string arithmatic program of SOS form.\n");
    
    input:
        printf("Input: ");
        scanf("%18s", input);
        for(k = j = i = 0; input[i]; ++i){
            if(islower(input[i])){
                if(j == 0)
                    a[2][k] = input[i];
                a[j][k++] = input[i] - 'a' + 1;
            } else if(strchr("+-/*", input[i])){
                b = input[i];
                ++j;//! Operator is assumed one
                k = 0;
            } else {
                //Illegal characters are present
                goto error_proc;
            }
        }
        if (b == '+') goto add;
        if (b == '-') goto sub;
        if (b == '/') goto div;
        if (b == '*') goto mul;
    
    error_proc:
        while(getchar()!='\n');
        goto input;
    
    add:
        for(i=0; i < k; ++i){//! k : it's assuming the length of the operand is equal
            if(a[2][i] + a[1][i] > 'z')
                a[2][i] = toupper(a[2][i]);
            else
                a[2][i] = 'a' + a[0][i] + a[1][i] - 1;
        }
        goto output;
    
    sub:
        goto end;
    div:
        goto end;
    mul:
        goto end;
    
    output:
        for(i = 0; i < k; ++i){
            c[i] = a[2][i];
        }
        c[i] = '\0';
        printf("%s", c);
    
    end:
        return 0;
    }