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;
    cout << z;

    return 0;

It should compile it...

3 Answers

Community On Best Solutions

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

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.

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


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