I am creating a simple calculator to do simple math in dec hex and octal froms , i am having trouble reading in an argument as a octal value, my hex is working fine when I enter 0x before the number but when reading in 010 for 8 in octal it just reads in 10, any suggestions?
thanksCode:#include<stdio.h> #include <stdlib.h> #include<string.h> int main(int argc, char *argv[]){ int a; int b; int c; int i; if (argc == 4 || (strcmp (argv[4], "dec") == 0 )){ if (strcmp (argv[2], "+") == 0){ a = atof(argv[1]); b = atof(argv[3]); c= a+b; printf("%d\n", c);} if (strcmp (argv[2], "-")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a-b; printf("%d\n", c);} if (strcmp (argv[2], "*")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a*b; printf("%d\n", c);} if (strcmp (argv[2], "/")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a/b; printf("%d\n", c);} if (strcmp (argv[2], "%")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a%b; printf("%d\n", c);}} if (argc == 5){ if ((strcmp (argv[4], "oct") == 0 )){ if (strcmp (argv[2], "+") == 0){ a = atof(argv[1]); b = atof(argv[3]); c=a+b; printf("%o\n", c);} if (strcmp (argv[2], "-")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a-b; printf("%o\n", c);} if (strcmp (argv[2], "*")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a*b; printf("%o\n", c);} if (strcmp (argv[2], "/")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a/b; printf("%o\n", c);} if (strcmp (argv[2], "%")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a%b; printf("%o\n", c);}} if ((strcmp (argv[4], "hex") == 0 )){ if (strcmp (argv[2], "+") == 0){ a = atof(argv[1]); b = atof(argv[3]); c=a+b; printf("%x\n", c);} if (strcmp (argv[2], "-")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a-b; printf("%x\n", c);} if (strcmp (argv[2], "*")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a*b; printf("%x\n", c);} if (strcmp (argv[2], "/")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a/b; printf("%x\n", c);} if (strcmp (argv[2], "%")==0){ a = atof(argv[1]); b = atof(argv[3]); c= a%b; printf("%x\n", c);}} } return 0; }



LinkBack URL
About LinkBacks



