Please i am not getting flow and i am new to C++.
Can any body help in this program.

C++ program to convert an octal number to decimal and from decimal back to octal.

#include <iostream>
#include <math.h>

using namespace std;

void octtodec();
void dectooct();

int main() { 
void octtodec(); 
void dectooct(); 
return 0;
}

void octtodec()
{
int oct,y,p,sum,x,m,i;

cout<<"Enter octal no : ";
cin>>oct;
for(i=1;m!=0;++i)
{
m=pow(10,i);
y=oct;
x=y%m;
p=x * pow(10,i-1);
sum +=p;
y/=10;
}

cout<<"the decimal value is "<<sum;
}


void dectooct()
{
int a[50],d,dec,count=0;

cout<<"Enter the decimal no";
cin>>dec;
for(int i=0;d!=0;++i)
{ 
d=dec;
a=d%8;
d/=8;
count+=1;
}

cout<<"The octal no is";

for(i=count-1;i>=0;++i)
{ 
cout<<a;
}
}

Recommended Answers

All 3 Replies

Mabey this function I made will help you:

template<class t> inline
char *toBase(t val, int base) {
	char d = 1; t c;
	if(val>=0)for(c=base;c<=val;c*=base,d++);
	else for(c=-base;c>=val;c*=base,d++);
	char *bin=new char[d+1];
	register char i=d;
	for(val*=base;i;i--)
	bin[i-1]=48+(val/=base,(char)
	((val%base)<0?-(val%base):(val%base)));
	bin[d]='\0';
	return bin;
}

You can use it do convert any number to a char array of any base from 2 - 10.
for example

toBase(97, 2) // output will be "1100001"

>Mabey this function I made will help you
You're allowed to use whitespace, you know. That code is pretty nasty with the current formatting.

Sorry :-/ kinda got into the habbit of doing that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.