Binary String to Decimal Number C++

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;
}
If you get a compilation error, the program is not even built, so whatever program you are running is an older version.

As for the error you are getting, in C++, arrays are as they are in C - that is, they are literally a group of the same kind of object laid one after another in memory. They have no members or methods, and they don't know their own size. You should use something like std::vector instead, or if you're not allowed to, track the size of the array with a separate variable.
In your binConv function, you don't need to use any global data. You have everything you need in the form of your function parameter 'binary', which is of type std::string.

In this function, you must be careful to remember that the way you're iterating through each character of the string is from most significant bit to least significant bit. Your call to pow is passing the wrong thing for the exponent. Take for example what would happen if you were to provide "1101" at the command prompt:
Current implementation:
1  1  0  1
20 21 22 23
= 11

What you should be doing:
1  1  0  1
23 22 21 20
= 13
Last edited on
Thanks for the responses, i've gotten the program running well, except it still doesn't perform the function. I changed the code for the pow function to
pow(2.0, size - counter + 1), which should run what booradley was talking about. When I run the program, it always says the decimal number is zero.
If you're still saying if (binNum[counter] == '1') then that's probably your problem. You don't actually ever do anything with the binNum array. You never fill it with anything. It sounds like your compiler is being very generous and initializing this memory to zero for you. However, you do have a handy parameter there named binary, which you stick the user input into when you call the function from main. Try if (binary[counter] == '1')
Last edited on
First you didn't print the return value of your function and second, you are using the wrong array. You should be using binary string parameter of your function rather than the random global array you are using.

Also this:
decimal = (decimal + pow(0.0,counter));

is pointless. What is 0546348578458936457848359454?
Last edited on
Topic archived. No new replies allowed.