how to read in octal

This is a discussion on how to read in octal within the C Programming forums, part of the General Programming Boards category; I am creating a simple calculator to do simple math in dec hex and octal froms , i am having ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    >> Scanf doesn't support octal(AFAIK)

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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:46 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21