0

When i want to compile a code, is giving me an error... cout and cin was not declared in this scope. What's the problem?

I searched on google. They said i need to reinstall codeblocks. I have done this and is not working.

#include <iostream>


int main()
{
    int n,z,c;
    cin>>n;
    z=0;
    while(n>0)
    {
        c=n%10;
        n=n/10;
        if(c<5)
        {
            z=z*10+2*c;
        }
    }
    cout << z;

    return 0;
}

It should compile it...

AmicuLL
  • 55
  • 7

3 Answers3

0

Just add this using namespace std; after #include <iostream> . Or use std::cin std::cout.

AmicuLL
  • 55
  • 7
0

Also posting the 3rd way (compromise between the 2 existing answers - was already mentioned in the comments), which I think works best for the current scenario. This is my favorite one (well, excepting cases when I use lots of stuff from a namespace).

Add:

using std::cin;
using std::cout;

after the #include. This way:

  • You avoid using namespace X; hell. That's a big NO-NO, there are lots of resources explaining why (you could check [SO]: what is the reason for using a wildcard import? (@CristiFati's answer) for an equivalent in Python)
  • You don't have to type the fully qualified name every time (just the plain name). Using FQNs can be / is:
    • Very annoying (especially when dealing with nested namespaces)
    • Safe
    • Easier to read
CristiFati
  • 38,250
  • 9
  • 50
  • 87
-1

Addding std::cin or std::cout would fix it If you don't want to add std:: again and again then You can also add using namespace std; just after #include<iostream>


This happens because cin and cout are members of standard library.

  • `using namespace std;` is probably not a good hint. This will bring the whole `std` namespace into the global namespace. `using std::cout; using std::cin;` is prefered instead. – mch Apr 23 '19 at 12:00
  • 1
    Check https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice please. – πάντα ῥεῖ Apr 23 '19 at 12:01