Hello, I have a project due tomorrow that I'm working on for my C++ course that's giving me some headaches. I need to input a binary string, and the program will output the correct decimal number. This task is to be repeated until the input value is -1, which displays the message "All Set!" and terminates the program.
The errors I'm getting are in the function, where i declare size = binNum.size(). The ide says:
- request for member 'size' in 'binNum', which is of non-class type
'int [1000]'
-Method 'size' could not be resolved
And when I run the program, the console behaves like this:
Enter the Binary String 1110
The equivalent Decimal Number is:
Enter the Binary String 1101
The equivalent Decimal Number is:
Enter the Binary String -1
The equivalent Decimal Number is:
All Set!
So it doesn't do the conversion, and it still displays the equivalent decimal number output before saying all set and terminating.
Any feedback will be tremendously helpful. Thanks for the help!
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
|
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
//prototype function
int binConv(string binary);
int binNum[1000];
int counter;
int size;
double decimal;
string binary;
int main()
{
while (binary != "-1")
{
if (binary == "-1") break;
else
{
cout << "Enter the Binary String ";
cin >> binary;
cout << "The equivalent Decimal Number is: ";
binConv(binary);
}
}
cout << "All Set!" << endl;
return 0;
}
int binConv(string binary)
{
decimal = 0;
size = binNum.size();
for (int counter = 0; counter < size; counter++)
{
if (binNum[counter] == '1')
decimal = (decimal + pow(2.0,counter));
else
decimal = (decimal + pow(0.0,counter));
}
return decimal;
}
|