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;
}