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
Printable View
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
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.
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;
}
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:for octal, it's a simple change, and for hex it's a simple change plus some, but it shouldn't be too hard.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;
}
edit: the meat of it is:then you have to reverse the string.Code:string=number%base;
number/=base;
>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.