Invalid Operands?

My programs goal is to input a binary number and output a decimal one. The textbook told us to base it off a previous excersize so I did. It is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
#include <bitset>
#include <string>
using namespace std;
int Binary;
int bin2Dec(const string& binaryString)
{
string BinaryNumber;
return 0;
}
int main()
{
string BinaryNumber;
cout << " Please enter a binary number " << endl;
cin >> BinaryNumber;
cout << " The binary number translated to decimal is " << bitset<32>(BinaryNumber).to_ulong() << endl;
}


It seems to work perfectly.
My job is to change it to use arrays.

I keep getting error : "invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'int')
Can someone 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
#include <iostream>
#include <bitset>
#include <string>


using namespace std;

int bin2Dec(const char binaryString[]);

const char binaryString[] = { };
int decimal;

int main()
{
    
    cout << "Enter a binary number: " << endl;
    
    
    for (int i = 0; i < 100; i++)
        cin >> binaryString[i]; // error is here
    
    decimal = bin2Dec(binaryString);
    
    cout << " The binary number translated to decimal is " << decimal << endl;
}

int bin2Dec(const char binaryString[])
{
    for(int i = 0; i < 100; i++)
        decimal = bitset<32>(binaryString[i]).to_ulong();
    
    return decimal;
    
}
Your binaryString was declared as an array of constant chars.

Also, the array currently has a size of 0, and there is no need to have your variables global. Keep them inside main() otherwise the variable name on line 27 shadows your global variable and variable used on line 30 is ambiguous.
I moved my variables into the main().
I still receive that error. I don't understand the significance of using constant chars. Help?
Thank you.
Constant variables cannot be modified. Simply remove the const qualifier, so you can change the values in your array. Also make sure your array has sufficient space to store the values.
1
2
char user_input[80] = "\0";
std::cin.getline(user_input, 80); //This is for reading character arrays 

http://www.cplusplus.com/reference/istream/istream/getline/?kw=istream%3A%3Agetline
Topic archived. No new replies allowed.