Reputation: 47
I have a basic program to add 2 binary strings. Given two binary strings a and b, add them together and return the resulting string. I am compiling it with a C++ compiler (G++ v8.4.0)
#include <iostream>
#include <string>
using namespace std;
std::string addBinaryStrings(std:: string a, std:: string b)
{
std::string result = "";
int s = 0;
int i = a.size() - 1, j = b.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
s += ((i >= 0)? a[i] - '0': 0);
s += ((j >= 0)? b[j] - '0': 0);
result = char(s % 2 + '0') + result;
s /= 2;
i--; j--;
}
return result;
}
int main()
{
string a,b;
getline(cin, a);
getline(cin, b);
cout << addBinaryStrings(a, b) << endl;
return 0;
}
When I execute this I get the following error :
main.cpp online 2:7: error: expected nested-name-specifier before ‘namespace’
using namespace std;
^~~~~~~~~
Where am I going wrong?
Upvotes: 2
Views: 11014
Reputation: 119
There is nothing wrong with this code as its working on my C++ GNU compiler. So there is something wrong with your compiler. I suggest you to reset settings of your compiler or use another compiler. You can also use online IDE.
Upvotes: -2