don't know why its not working
Jan 15, 2017 at 11:52am UTC
Hkdk
Last edited on Jan 16, 2017 at 7:59am UTC
Jan 15, 2017 at 2:07pm UTC
You have tried to define your decimalToOctal function inside the main function. You can't define a function inside another function. So you have to move lines 126-138 to the bottom of the file.
The same is true for function convertBinaryToOctal(). Lines 71-93 must move to the bottom of the file. In addition, you'll need to add
return octalNumber;
to the end of that function.
Line 118:
nt
should be
int
.
I would end my advice there, but that would result in a couple of misplaced braces that cause tons of syntax errors, so I'm going to give you the full code. I've also added consistent indenting. This is really important to help you keep the code straight in your head. Compilers use the braces to indicate the code structure, but humans tend to use the indentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#include<iostream>
#include<cmath>
#include<string>
#include<iomanip>
using namespace std;
//prototypes
double BinarytoInteger(double Binary);
double BinarytoOctal(double Binary);
double IntegertoBinary(double Interger);
double IntegertoOctal(double Integer);
int convertBinarytoOctal(long long );
int decimalToOctal(int decimalNumber);
int convertBinarytoOctal(long long );
///Entry point to the application
int
main()
{
//display a menu
cout << "Menu" << endl;
cout << "1) Convert Binary to Integer" << endl;
cout << "2) Convert Binary to Octal" << endl;
cout << "3) Integer to Binary" << endl;
cout << "4) Integer to Octal" << endl;
cout << "5) Exit" << endl;
//get user's choice
cout << "Enter your choice:" ;
int choice = 0;
cin >> choice;
//cases
switch (choice) {
case 1:
{
string n;
cout << " Enter Your Binary number: " ;
cin >> n;
n = string(n.rbegin(), n.rend());
int dec_num = 0;
for (int i = 0; i < n.size(); i++) {
int dec_n = n[i];
int dec_value = dec_n - '0' ;
dec_num = dec_num + dec_value * pow(2, i);
}
cout << " Integer number is: " << dec_num;
break ;
}
case 2:
{
long long binaryNumber;
cout << "Enter a binary number: " ;
cin >> binaryNumber;
cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber)
<< " in octal " ;
return 0;
}
break ;
case 3:
{
int arr[100], i = 0, j;
int n;
cout << "enetr positive number: " ;
cin >> n;
while (n > 0) {
arr[i] = n % 2;
i++;
n = n / 2;
}
cout << "binary nimber is: " ;
for (j = i - 1; j >= 0; j--) {
cout << arr[j];
}
break ;
}
case 4:
int decimalNumber;
cout << "Enter a decimal number: " ;
cin >> decimalNumber;
cout << decimalNumber << " in decimal = " << decimalToOctal(decimalNumber) <<
" in octal" ;
return 0;
case 5:
{
cout << "Goodbye" << endl;
break ;
}
}
return 0;
}
int
convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;
while (binaryNumber != 0) {
decimalNumber += (binaryNumber % 10) * pow(2, i);
++i;
binaryNumber /= 10;
}
i = 1;
while (decimalNumber != 0) {
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
// Function to convert decimal number to octal
int
decimalToOctal(int decimalNumber)
{
int rem, i = 1, octalNumber = 0;
while (decimalNumber != 0) {
rem = decimalNumber % 8;
decimalNumber /= 8;
octalNumber += rem * i;
i *= 10;
}
return octalNumber;
}
Topic archived. No new replies allowed.