I need to make this... to input an octal number and it would output the binary like converting the octal number to a binary... thisi s my code so far.. yet it wont run.. please help me in this problem.
#include"stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
void binary(int);
void main(void) {
int number;
cout << "Please enter a octal integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
binary(number);
cout << endl;
things to consider 1) main should be int main not void main 2) match the "{" and "}" 3) indent the code well so that you and forum members can read and understand it well 4) post the code in the forum encasing it in the code tag
okay... so i fixed my code...
here'es my new one :
void dec2bin ( int num )
{
int remainder;
if(num <= 1) {
cout << num;
return;
}
remainder = num%2;
dec2bin(num >> 1);
cout << remainder;
}
void dec2oct( int num )
{
int i, n, r, b = 0;
n = num;
i = 0;
while(n != 0 ) {
r = n % 8;
b = b+pow(10.0,i)*r;
n = n/8;
i++;
} // end while
cout << "Octal number is: " << b << endl;
}
void dec2base(int num, int base) {
if (num >= base) { dec2base(num/base, base); }
cout << num % base;
}
when i search all over the net... it always have a "void " function and not "int main" but the only problem here is that... conversion from 'double' to 'int', possible loss of data that's what it says. Please help me run this program.
If its complaining about line b = b+pow(10.0,i)*r; then its about the pow function, it returns a double and b is an int.
That is the reason compiler is complaining. If you are not worried about large numbers as input you can typecast the result of pow to int. Otherwise you will have to change b to double.