-2

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);
}
aelius
  • 3
  • 1
  • 2
    Consider what makes a good ["Minimal, Complete, and Verifiable Example"](http://stackoverflow.com/help/mcve). You've posted an entire program and the problem you were given, when what you want is to understand a crash. Would it still crash if your program only did addition? If so, why should there be code for other operations in your question? Why are you showing us you printing out a program "banner"...do you have the question without it? The header comment? Try reducing your program down to an essential case that shows the crash, you might solve it yourself in the process... – HostileFork says dont trust SE Oct 01 '14 at 22:43
  • 1
    `j, k` need initialize. – BLUEPIXY Oct 01 '14 at 22:46
  • You claim to be more experienced in Java and you use `goto` in C. Right... – EOF Oct 01 '14 at 22:48
  • The reason that there is other code is because the program is expected to be able to perform the arithmetic when the user inputs +-\ or *. I haven't added the code for any other function besides add because I want to get that working first – aelius Oct 01 '14 at 23:57
  • And I don't really understand how goto is an advanced function of C. I think I learned it from the internet. Its very similar to using jump-and-link in MIPS – aelius Oct 02 '14 at 00:07
  • No, `goto` is like `j/jr/(unconditional)b`. `jal / jalr` (jump and link (/register)) is a function/subroutine call. `goto` isn't advanced, it's primitive. There are legitimate uses for it even in modern C, but your program is not an example of any of them. – EOF Oct 02 '14 at 00:54
  • Well, I'm still stranded on this error. Any advice? – aelius Oct 02 '14 at 01:25
  • `input[i] != '+' || input[i] != '-' || input[i] != '/' || input[i] != '*'` wrong. (always true) – BLUEPIXY Oct 02 '14 at 08:26
  • Misunderstanding there are many others. – BLUEPIXY Oct 02 '14 at 08:35

2 Answers2

1

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

One immediately seen problem is that neither j nor k are initialized. I don't know Java, but in C an uninitialized variable is not set to 0 -- it contains a garbage value. The net result is an undefined behaviour. I cannot explain why does it fail at line 44 (and not at line 23), but that's a nature of UB.

Fix the initialization and see if the problem persists.

user58697
  • 7,808
  • 1
  • 14
  • 28
  • They are not. They are merely declared: `int j, k;`. To be initialized they must be explicitly assigned a value: `int j = 0;` for example. – user58697 Oct 01 '14 at 23:59
  • When I declare j and k as variables they should default to null. Regardless, I tried what you said to do and it didn't change the outcome. – aelius Oct 02 '14 at 00:01