Thread: how to read in octal

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    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. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by User Name: View Post
    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. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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));
    
         return total;
    }
    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. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by User Name: View Post
    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));
    
         return total;
    }
    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. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Scanf doesn't support octal(AFAIK)

    Use %o -- see this page for a source.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > im not using scanf, we were told not to i am reading them in as arguments
    So use sscanf() then.

    Why ARE you using atof() when you should have used atoi()? Which should have done the octal magic for you.
    Floating point doesn't do bases very well.

    If you really want to make it robust, then use strtol().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  2. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

Tags for this Thread