I'm making a program to add/sub/mult numbers from bin/oct/dec/hex format and output in whatever base the user asks, run syntax:
./instruction <num1> <num2> <base1> <base2> <output base>
and, besides the stupid implicit declaration warnings I get for some unknown reason (I'd love to know why), when I run this (I'm just trying to test conversion from dec to binary right now) I get an "Illegal Instruction" error when I try and reverse the temporary array and dump it into the answer array. What's wrong with it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void toNeg(int num);
int main(int argc, char *argv[]){
int x = 0, y = 0, base1, base2, outbase;
char num1[32], num2[32], bin1[32], bin2[32];
/*unsigned int unum1, unum2, neg1, neg2;*/
if(strcmp(argv[0],"./add")) x = 0;
else if (strcmp(argv[0],"./sub")){
x = 0;
y = 1;
}else if (strcmp(argv[0],"./mult")) x = 1;
else x = 2;
/* 0 = dec, 1 = bin, 2 = hex, 3 = oct */
base1 = checkBase(argv[3]); /*implicit declaration?*/
base2 = checkBase(argv[4]);
outbase = checkBase(argv[5]);
if(base1 == -1 || base2 == -1 || outbase == -1){
fprintf(stderr, "One or more of the arguments entered is invalid\n");
exit(1);
}
strcpy(num1, argv[1]);
strcpy(num2, argv[2]);
printf("dec: %s\n",num1);
toBin(num1, base1, bin1); /*implicit declaration?*/
toBin(num2, base2, bin2);
printf("bin: %s\n",bin1);
switch(x){
case 0:
if(y == 1){
/*toNeg(unum2);*/
}
}
return 0;
}
int checkBase(char str[]){
if(strcmp("dec", str)==0){
return 0;
}else if(strcmp("bin", str)==0){
return 1;
}else if(strcmp("hex", str)==0){
return 2;
}else if(strcmp("oct", str)==0){
return 3;
}
return -1;
}
/**************/
void toBin(char *num, int base, char *bin){ /*conflicting types for toBin?*/
char temp[32];
int old;
unsigned int i=0, j=0, n, remain;
n = atoi(num);
if(base == 0){
do{
old = n;
remain = n%2;
n = n/2;
printf("%d/2 = %d remainder = %d\n", old, n, remain);
temp[i++] = remain + '0';
printf("%s\n",temp);
}while(n > 0);
/*reverse*/
while(i >= 0) bin[j++] = temp[--i]; /*<-------THIS!!!---*/
}else if(base == 2){
}
}