-3

Good Day Guys. So For an Assignment, I was tasked with creating 6 classes of varying functions. I, however, am having trouble with 1 in particular, which requires me to make a class that takes a binary number and convert it into a decimal. Below is what I have from the assignment provided driver file:

     #include <iostream>
     using std::cout;
     using std::cin;
     using std::endl;


    #include "binaryconversion.h"

    int main()
    {
    ///*********************************Testing BinaryConversion                     Class************************************************/
    cout<<"Creating BinaryConversion object\n\n\n";
    BinaryConversion binary;
    cout<<"Binary value 111 to decimal = 3\n";//7
    cout<<"Actual function result: " << binary.binaryToDecimal(111);
    cout<<"Binary value 101001 to decimal = 41\n";
    cout<<"Actual function result: " << binary.binaryToDecimal(101001);
    cout<<"Binary value 11100 to decimal = 28\n";
    cout<<"Actual function result: " << binary.binaryToDecimal(11100);

   return 0;
   }

For the .h file I have this:

   #ifndef BINARYCONVERSION_H
   #define BINARYCONVERSION_H

   #include <iostream>
   using std::cout;
   using std::cin;
   using std::endl;

   class BinaryConversion
   {
   public:
         BinaryConversion();
         void binaryToDecimal(int);


   };

    #endif // BINARYCONVERSION_H

And finally, for the .cpp, I have this:

    #include "binaryconversion.h"

    BinaryConversion::BinaryConversion()
    {

    }

    void BinaryConversion::binaryToDecimal(int){

        int decimalNumber = 0, y = 0, remainder;
        while (x!=0)
        {
            remainder = n%10;
            n /= 10;
            decimalNumber += remainder*pow(2,y);
            ++y
        }
        return decimalNumber;
    }

Now my problem is, that when I build the file, I get errors along the following lines in the driver file:

cout<<"Actual function result: " << binary.binaryToDecimal(111);


cout<<"Actual function result: " << binary.binaryToDecimal(101001);


cout<<"Actual function result: " << binary.binaryToDecimal(11100);

What could I possibly have done wrong?

edit: current code is as follows after following all of your suggestions of correction :

    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    class BinaryConversion
    {
    public:
        BinaryConversion();
        int binaryToDecimal(int x);

    private:
        int x;
        int n;


    };

    #endif // BINARYCONVERSION_H

.cpp:

    int BinaryConversion::binaryToDecimal(int x){

       int decimalNumber = 0, y = 0, remainder;
        while (x!=0)
        {
            remainder = x%10;
            x /= 10;
            decimalNumber += remainder* 1 >> y;
            ++y;
        }
        return decimalNumber;
    }
  • 2
    `void` as a return type means you promise not to return any value at all. Then you go and `return decimalNumber;`, breaking the promise. One or the other has to change so that they agree. – Ben Voigt Nov 02 '16 at 02:23
  • so i should remove void and give it something like bool? – Alexis Nonya Nov 02 '16 at 02:24
  • Just a side note, `1 << y` is a much much faster way of computing 2-to-the-power-of-y when `y` is an integer, than calling the `pow` function which works on floating-point numbers. – Ben Voigt Nov 02 '16 at 02:24
  • Yes, the `void` needs to change. But figure out what your return type is, don't guess. (And `bool` is not correct anyway) – Ben Voigt Nov 02 '16 at 02:25
  • ah I see. so if I figure out what return type it will be, then the program should perform as desired. okay, let me go and try and figure that out and I'll get back to you. – Alexis Nonya Nov 02 '16 at 02:29
  • @AlexisNonya [Do not use pow() with integer exponents](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). There is no guarantee that `pow()` will return the "correct" answer. – PaulMcKenzie Nov 02 '16 at 02:32
  • @PaulMcKenzie oh okay, well I was fixing up the code based off of suggestions and the pow() says that it wasn't declared in the scope anyways. So if I use Ben Voigt's suggestion of using 1 << y instead, it will be a suitable replacement? – Alexis Nonya Nov 02 '16 at 02:38
  • Try replacing `pow(2,n)` with `1 << n`. Look up the left shift operator. – Thomas Matthews Nov 02 '16 at 02:42
  • so I replaced the operator, and for the binary value 111, it gave me 3. – Alexis Nonya Nov 02 '16 at 02:45
  • it also only produces only 1 of the 3 test operators needed to be outputted – Alexis Nonya Nov 02 '16 at 02:47
  • You've got more mistakes in your code, so you must not be using the same exact code. – Ben Voigt Nov 02 '16 at 02:50
  • I managed to get it working. Thanks to all of you who had the patience to help me see how to fix it. TThanks you :D – Alexis Nonya Nov 02 '16 at 03:50
  • @AlexisNonya Is there a reason `binaryToDecimal()` is a method of a class instead of just a free function? – Mark H Nov 02 '16 at 04:47
  • it was part of an assignment. We had to make six separate classes. This was one of them. It was also the last one. I was supposed to work it with a partner but she got sick and I had to solo it(for the most part since you guys helped me :D) – Alexis Nonya Nov 08 '16 at 01:32

1 Answers1

0

Your void BinaryConversion::binaryToDecimal(int) function isn't correctly implemented.Firstly, you passed a value to the function but parameter name was't correctly written. It should be binaryToDecimal(int x).For this reason you get this error.

Secondly, The return type shouldn't be void because it return an integer value to the calling function. So return type should be int .

Function should be look like the following :

void BinaryConversion::binaryToDecimal(int x){
int decimalNumber = 0, y = 0, remainder;
     while (x!=0){
     remainder = x%10;
     x /= 10;
     decimalNumber += remainder*pow(2,y);
     ++y;
   }
   return decimalNumber;
}
Sakib Rahman
  • 333
  • 2
  • 13
  • SO under the header, the code should read as int binaryToDecimal(int x); and then the function should be as you placed it there ? – Alexis Nonya Nov 02 '16 at 02:55
  • my results came out as Binary value 111 to decimal = 3 Actual function result: 1Binary value 101001 to decimal = 41 Actual function result: 1Binary value 11100 to decimal = 28 Actual function result: 0Press – Alexis Nonya Nov 02 '16 at 02:56
  • You just change the code consists in the function body. – Sakib Rahman Nov 02 '16 at 03:00
  • i think i did something wrong, because i got 4 errors now that i replaced the function with your code. – Alexis Nonya Nov 02 '16 at 03:08
  • What type of error. You should read the error carefully and try to resolve – Sakib Rahman Nov 02 '16 at 03:11
  • after reading over the errors, they are as follows: no match for 'operator<<'(operand types are 'std::basic_ostream'and'void') std::cout<<"Actual Function result: " << binary.binaryToDecimal(111); no match for 'operator<<'(operand types are 'std::basic_ostream'and'void') std::cout<<"Actual Function result: " << binary.binaryToDecimal(101001); no match for 'operator<<'(operand types are 'std::basic_ostream'and'void') std::cout<<"Actual Function result: " << binary.binaryToDecimal(111); and "forming reference void" – Alexis Nonya Nov 02 '16 at 03:24
  • when you define the class there the function name is also should be `int binaryToDecimal(int x);` – Sakib Rahman Nov 02 '16 at 03:34
  • okay , I managed to get it working. Thanks you so much for all your assistance and patience :D – Alexis Nonya Nov 02 '16 at 03:49