Thread: Converting Octal to decimal

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    19

    Lightbulb Converting Octal to decimal

    I am clueless on this assignment I dont know how to use scanf yet so I have to use chars
    insert
    Code:
    #include<stdio.h>
    main()
    {
        printf("Please enter an octal number ending with # \n");
        char a,b,c,d,e=0;
         while((d=getchar())!='\n')
         {
           while(d!='\n')
           {
            a=c-'0';
            char k=getchar();
            if(k=='#')
           {
               int f=a,c;
               octal(f);
               break;
           }
           }
         }
    }
    
    
    
    
    void octal(int f)
    {   int decimal=1, i=0, rem=1;
        f=0;
        while (f<=3)
        {
         decimal=rem* pow(8,i++);
         printf("The converted value is %d\n",decimal);
        f++;
            printf("%d\n",i);
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your code appears to have signs of "writing it off the top of your head". Moreover, there are several serious warnings I received when trying to compile your code, suggesting it was written out in one go rather than built up and tested incrementally.

    See: A development process

    I'd recommend starting over ... and not starting with the code. Start by developing a plan, with pencil and paper. Figure out step by step how you can accomplish this task. A flow chart would help you structure your logic before you write any code.

    When you have a plan for the flow of logic, begin translating it into code - a little bit at a time. Write a bit of code, compile, take care of warnings/errors, and test. Then, and only then, do you start to add a little bit more code; compile, test, etc.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Code:
    while((d=getchar())!='\n')
         {
           while(d!='\n')
           {
            a=c-'0';
            char k=getchar();
            if(k=='#')
           {
               int f=a,c;
               octal(f);
               break;
           }
           }
         }
    you have a redundant statement in your while loops which is unnecessary

    Code:
    a=c-'0';
    you make use of the variable c, which you have not initialized.

    oh boy.... i cant do this...i think you should try to compile this code,your compiler will refuse,read why it refuses and then when you understand that get back to the drawing board and write down(draw..lol) what you want to do(pseudo-code) step by step. Once you have done all that, try to write again and we'll start from there

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    Okay I thought about it a little more and planned it out and came up with this why are values in the 40 printting out
    Code:
    #include<stdio.h>
    main(){
    
    
        long int decimal =0;
        int i=0;
        printf("Please enter an octal number ending with # \n ");
        char octal;
        octal=getchar();
        while(octal!=0){
             decimal = decimal + (octal % 10) * pow(8,i++);
             octal = octal/10;
        }
        printf("The converted value is: %d",decimal);
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, this time around, you're not converting the value from a character (e.g. '4') to an integer (e.g. 4).

    Also, the program is also only capable of converting a single digit. The only valid single-digit octal values are 0 - 7, and these map directly to base-10 values 0 - 7 (i.e. there is no real conversion that can take place).

    Okay, let's figure out a top-level plan.

    1. Get user input (octal value)
    2. Validate user input (no "column" can have a value over 7)
    3. Convert to decimal



    1. Get user input (octal value)

    As already discussed, a single "getchar()" call will not be able to get an "octal number" larger than a single digit. You have a few options here.

    One would be to continue using "getchar()" in a loop as your first program did - however, your first program also processed them a digit at a time in a conversion loop designed to process the entire value. You would have to think about how to read many characters, and systematically update an integer to represent the additional digits as they are entered, before the entire value is converted. However, this is needlessly complex to implement.

    Another would be to use "scanf()" - not the safest function, especially for a beginner, but it should work fine for "academic" purposes. You stated that you don't know "scanf()" yet - but you do know that it exists. It would only take an hour or so to (1) learn how to use it, and (2) do some small sample programs to get the feel for how to work with it. This would greatly simplify the "get user input" portion of your assignment.

    You can find more in-depth explanations, with examples, here: FAQ > How do I get a number from the user (C) - Cprogramming.com



    2. Validate user input (no "column" can have a value over 7)

    This could be considered an "optional" step, but is good to practice. In your latest code, you show that you know how to "peel" individual digits from a number and test them (the modulus/division approach). You can use this technique (on a copy of the original input - you don't want to destroy the input value during validation before you actually convert it) to validate the input.



    3. Convert to decimal

    The final piece of the puzzle - the "real" part of the assignment. Now the fun begins.

    At this point, you should have both programmed and thoroughly tested the first two steps. So now you can be fairly sure that any value you're working on is a valid octal value.

    Before I spend my time launching into this, however, it would make me feel better to see an attempt at the first two steps. I'll approach my assistance here like I approach my programs - wait until I see one portion working before moving onto the next.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    Okay so for step one would I put the getchar inside the whileloop?
    step 2 are you implying I would have to use an Array?

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    Code:
    while((octal=getchar())!=0)
        {
             decimal = decimal + (octal % 10) * pow(8,i++);
             octal = octal/10;
        }
    do you mean this

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    and the inside the while loop doesn't that do the conversion

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Okay so for step one would I put the getchar inside the whileloop?
    No - if you went this route, you'd need two separate loops, as you are doing two very different things. (1) Getting a value from the user; (2) Processing that value. These are completely separate goals, and as such need to be done separately. This is what programming is all about - breaking up the problem into small sub-problems, and solving them one at a time.

    However, my actual recommendation would be to read the link I posted and not use "getchar()" ("option 2" would probably be a good way for you to go).

    step 2 are you implying I would have to use an Array?
    You don't need an array - just a temporary value (as one approach) would do.

    - copy the entire value into temp
    - peel off the right-most digit with %
    - make sure that digit is valid
    - divide temp by 10
    - repeat

  10. #10
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    The problem is if I never went over scanf in class I dont want my professor to think im going a head or think im cheating when I just want help. I feel like I may need to do two separte loops. Oh okay I understand the 2nd part

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by lavaboy View Post
    and the inside the while loop doesn't that do the conversion
    I don't think you understand what I'm getting at. We are not concerned with doing any conversions at this point. We are simply trying to read a value from the user. Conversion will come later.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by lavaboy View Post
    The problem is if I never went over scanf in class I dont want my professor to think im going a head or think im cheating when I just want help. I feel like I may need to do two separte loops. Oh okay I understand the 2nd part
    That is an understandable stance - though it arguably could show that you're just reading ahead. Out of curiosity, have you had to write code to receive a (multi-digit) number from the user for previous assignments? If so, how was this done?

  13. #13
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    yes I've had to write a program as a calculator
    like 3+4=7
    where I read in two digits from the keyboard and the computer computes the values would I do a similar approach

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    And you used "getchar()" (and "minus '0') for that program?

  15. #15
    Registered User
    Join Date
    Mar 2014
    Posts
    19
    yes I did
    c=getchar() -'0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Decimal to Binary
    By BeldenML in forum C Programming
    Replies: 17
    Last Post: 02-09-2012, 11:29 AM
  2. Decimal, Octal & Hexadecimal
    By mie2cpr in forum C Programming
    Replies: 2
    Last Post: 03-01-2011, 10:56 AM
  3. Converting from ternary to decimal
    By kgehrma in forum C Programming
    Replies: 19
    Last Post: 02-08-2011, 05:29 PM
  4. Converting an octal code to Char
    By winsonlee in forum C Programming
    Replies: 3
    Last Post: 03-20-2004, 05:54 PM
  5. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM