octal numbers

This is a discussion on octal numbers within the C++ Programming forums, part of the General Programming Boards category; ok i wont to make a program that converts octal to decimal. what i need to do is read a ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    octal numbers

    ok i wont to make a program that converts octal to decimal.
    what i need to do is read a number like 1256 and brake it up.
    Code:
    so it will say ok your number was 1256.
    than do
    1*8^3
    than
    2*8^2
    than
    5*8^1
    than
    6*8^0

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so how do you think you'd do it? There are many ways to do this. It's fairly easy. You can use simple division, or you can use strings. That'll get you started. Take your pick.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, here you go:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int num;
    
       cout << "Enter an octal number to be converted: ";
       cin >> oct >> num;
       cout << "Octal: " << oct << num << "  Decimal: " << dec << num << endl;
    }

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    heh... nice swoopy :P

    but since that only works on output, or just in case you wanted to know, here's a hint - this is how you would convert a decimal number to binary:
    Code:
    #include<iostream>
    #include<cstring>
    
    char*reverse(char*in);
    
    int main()
    {
            int i;
            const int base=2;
            char*out=new char[20];
            short int x=0;
    
            std::cout<<"Enter a decimal: ";
            std::cin>>i;
            std::cin.ignore(1);
    
            while(i>0)
            {
                    out[x++]=static_cast<char>((i%base)+'0');
                    i/=base;
            }
            out[x]='\0';
            std::cout<<"That number in binary  is: "<<reverse(out)<<std::endl;
            return 0;
    }
    
    char*reverse(char*in)
    {
            int begin;
            int end;
            char temp;
    
            for(begin=0,end=strlen(in)-1;begin<end;begin++,end--)
            {
                    temp=in[end];
                    in[end]=in[begin];
                    in[begin]=temp;
            }
    
            return in;
    }
    for octal, it's a simple change, and for hex it's a simple change plus some, but it shouldn't be too hard.

    edit: the meat of it is:
    Code:
    string=number%base;
    number/=base;
    then you have to reverse the string.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but since that only works on output, or just in case you wanted to know
    Agreed, knowing the algorithm certainly helps to understand better, and as you implied, you can convert to any base.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 05:03 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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