-1

Here's the code....

#include <iostream>

int main()
{
    cout << "WELCOME TO C++ PROGRAMMING";
    return 0;
}

And when I go the terminal and pass the command..

g++ hello.cpp

It shows...

hello.cpp: In function ‘int main()’:
hello.cpp:4:2: error: ‘cout’ was not declared in this scope
  cout << "WELCOME TO C++ PROGRAMMING";
  ^
hello.cpp:4:2: note: suggested alternative:
In file included from hello.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output

So what's the reason? And what should I do?

Heich-B
  • 672
  • 1
  • 6
  • 15
  • 1
    It is called `std::cout`. – juanchopanza May 26 '15 at 21:58
  • 2
    __Your code clearly does not produce the error in your question!__ Note that the string in the error is not found anywhere in your code. – Bill Lynch May 26 '15 at 21:59
  • `So what's the reason? And what should I do?` Probably quit reading old C++ books from the late 80's or early 90's. – PaulMcKenzie May 26 '15 at 22:03
  • Better read this too: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – juanchopanza May 26 '15 at 22:04
  • Some answers advise you to use `using namespace std;`. **DONT** use it. Read [here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) why you shouldn't, or save this link to read once you can understand the answers. – Slyps May 26 '15 at 22:04
  • 1
    __Your code _still_ clearly does not produce the error in your question!__ Note that the string is on line 5, while your error says that it is on line 4. I really __really__ dislike it when people post error messages from some other piece of code. – Bill Lynch May 26 '15 at 22:20

4 Answers4

8

cout is found in the std namespace.

#include <iostream>

int main()
{
    std::cout << "Welcome";
    return 0;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
4

To avoid name collisions, the C++ library is included in a namespace, named std. So to get your program to compile, either you add:

using namespace std;

at the top of your program, either you prefix each "object" of the standard library with std:::

std::cout << "Welcome";
kebs
  • 6,387
  • 4
  • 41
  • 70
  • 2
    Please don't give that `using namespace std` bad advice to beginners. – juanchopanza May 26 '15 at 22:02
  • Even then why is "Welcome" not printed? – Heich-B May 26 '15 at 22:03
  • @juanchopanza I agree on the bottom line, but, well, we all started by doing this, didn't we ? And by gaining experience, we slowly learned that it's in fact a bad idea. But from experience, learning c++ is already hard enough, so lets let them do that. – kebs May 26 '15 at 22:07
  • But not in the terminal! – Heich-B May 26 '15 at 22:14
  • @Myni Change it to `std::cout << "Welcome" << std::endl;`. The `std::endl` has the side effect of flushing your input ( or output depending on where you look from ). Since your program exits shortly after so it should be printed anyway, but I don't know what environment you run your executable in. Give it a try. – Slyps May 26 '15 at 22:16
  • @Myni Mmm, let me guess: Windows, I assume ? The console probably gets closed before you can see anything. Try to run it from the command line. – kebs May 26 '15 at 22:16
  • I don't have to. I don't tell beginners to write things like `using namespace std`. – juanchopanza May 26 '15 at 22:20
  • I use Deepin 2014.3 not Windows! So I run in Deepin's Terminal. – Heich-B May 26 '15 at 22:21
  • @juanchopanza oh, and most of them will not become professional programmers anyway, so while I fully agree with the issues [raised here](http://stackoverflow.com/questions/1452721), they'll never get close to the issues described. And for those who will get deeper, they will have time learning good practices. – kebs May 26 '15 at 22:25
  • @Myni So have you tried what I told you in my comment ... ? – Slyps May 26 '15 at 22:25
1

You should use std before cout

new commer
  • 11
  • 3
0

You should have:

std::cout << "Welcome";
Good Luck
  • 1,104
  • 5
  • 12
  • But still why isn't "Welcome" printed? – Heich-B May 26 '15 at 22:08
  • 1
    I just noticed that you have another problem....you have "Welcome" in the code and you get error with "WELCOME TO C++ PROGRAMMING"... are you sure you save your files before re-compiling? – Good Luck May 26 '15 at 22:10