1. ## how to read in octal

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?

Code:
#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;
}
thanks

2. Read it as a string and parse from there. Scanf doesn't support octal(AFAIK) and it isn't too hard to convert anyway.

3. Originally Posted by User Name:
Read it as a string and parse from there. Scanf doesn't support octal(AFAIK) and it isn't too hard to convert anyway.
im not using scanf, we were told not to i am reading them in as arguments

4. Sorry, didn't RTFS.

Okay... The problem is with atof. You'll have to implement your own I guess. For octal:
Code:
int octal(char *num)
{
int total = 0;
for(int digit = 0;num[digit] != '\0';digit++)
total += (num[digit] - '0') * (int)pow(8, strlen(num) - (digit + 1));

}
Is your class at a point where this would be reasonable, or is this "too advanced" and they are likely looking for something else?

5. Originally Posted by User Name:
Sorry, didn't RTFS.

Okay... The problem is with atof. You'll have to implement your own I guess. For octal:
Code:
int octal(char *num)
{
int total = 0;
for(int digit = 0;num[digit] != '\0';digit++)
total += (num[digit] - '0') * (int)pow(8, strlen(num) - (digit + 1));

}
Is your class at a point where this would be reasonable, or is this "too advanced" and they are likely looking for something else?

sounds a little complicated but not aweful, is there no simpler way?

6. >> Scanf doesn't support octal(AFAIK)