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 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.
Hope is the first step on the road to disappointment.
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;
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
>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.