2

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?

ITsme
  • 37
  • 1
  • 6
  • Your program compiles fine. But read this: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – JCWasmx86 Aug 11 '20 at 11:27
  • 1
    As can be seen [here](https://godbolt.org/z/E19zbj), there's nothing wrong with the code you've posted. So you're doing something wrong but it's not the code. – john Aug 11 '20 at 11:31
  • Taking a guess, are you compiling C++ code as C? More details of exactly what you are doing are needed, because it's not the code. – john Aug 11 '20 at 11:32
  • @john I am compiling it with a C++ compiler (G++ v8.4.0) – ITsme Aug 11 '20 at 11:35
  • So, more details, what command line are you using? – john Aug 11 '20 at 11:36
  • @john I am not really sure about that. – ITsme Aug 11 '20 at 11:37
  • So **describe what you are doing**. Clearly you aren't typing commands on the command line. Are you using an IDE? Just say what you did immediately before you saw this error message. – john Aug 11 '20 at 11:38
  • Clearly the problem here is that you are using your compiler incorrectly. But without any details about how you are using your compiler it's impossible to say what is wrong. – john Aug 11 '20 at 11:40
  • @john I am using an IDE, i am doing all of this online in a VSCode Editor. – ITsme Aug 11 '20 at 11:40
  • @ITsme OK the notorious VSCode. Almost certainly you have set it up incorrectly. If you can't figure it out for yourself, then you need to post the details of your various configuration files, so people who know a bit about VSCode will be able to advise. – john Aug 11 '20 at 11:42
  • @ITsme But my advice would be to find something easier to use than VSCode. – john Aug 11 '20 at 11:43
  • @john Thanks buddy, will do. – ITsme Aug 11 '20 at 11:44
  • @ITsme Wait `online` I missed that. Can you post a link to the web site you are using. – john Aug 11 '20 at 11:44
  • @john https://app.codesignal.com/ – ITsme Aug 11 '20 at 11:46
  • @ITsme So there doesn't seem to be any way I can try that out for myself without going through a whole load of tutorials. All I can advise is that somehow you are using the website incorrectly. But exactly how I can't say. – john Aug 11 '20 at 11:58

1 Answers1

-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Karan Kamboj
  • 121
  • 1
  • 5