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...

3 Answers

0
Community On Best Solutions

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

-1
Vivek Singh On

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.

0
CristiFati On

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