-2

I wrote the following code after using "gedit take_input.cpp":

#include <iostream>

int main() 
{
cout<<"please enter your name (followed by 'enter')\n";

string file;

cin >> file;

cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}

However, when I used "g++" to convert my human-readable code into object code (writing g++ take_input.cpp -o take_input), the terminal returns with a result similar to this:

take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
 cout<<"please enter your name (followed by 'enter')\n";
 ^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
 string file;
 ^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
                 from /usr/include/c++/4.9/ios:38,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note:   ‘std::string’
   typedef basic_string<char>    string;   
                                 ^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
 cin >> file;
 ^            ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
 cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note:   ‘std::cin’
   extern istream cin;  /// Linked to standard input
                  ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
 cin >> file;
        ^

Could you tell me what the reason is?

Jamal
  • 763
  • 7
  • 22
  • 32
  • 1
    You have to write `std::cout`, because it's in the `std` namespace. – πάντα ῥεῖ Jun 07 '15 at 11:49
  • `cout` does not live in the global namespace but in `std`, thus you either have to `using namespace std;` or write `std::cout` – 463035818_is_not_an_ai Jun 07 '15 at 11:49
  • 2
    btw you can be happy that you were not allowed to post pictures. I am pretty sure you would get even more downvotes if you did so. Pictures are not copy/pasteable, thus when you have some code, always put is as text not as picture. – 463035818_is_not_an_ai Jun 07 '15 at 11:52
  • I don't get it. Why was my question downvoted? Could anyone explain? New to StackOverflow here. –  Jun 07 '15 at 11:54
  • Add `using namespace std;` after your include files. This will also do the trick. – Nivetha Jun 07 '15 at 11:56
  • 2
    @SadmanSakib _"I don't get it. Why was my question downvoted?"_ Because you have shown very low research efforts. This is already covered by very basic tutorials and C++ text books. – πάντα ῥεῖ Jun 07 '15 at 11:57
  • 4
    @Nivetha [Please stop teaching this to beginners](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)! – Baum mit Augen Jun 07 '15 at 11:57
  • @πάνταῥεῖ thanks man. Will be careful next time :) –  Jun 07 '15 at 12:03
  • 1
    @SadmanSakib De nada. You might also go through [The tour](http://stackoverflow.com/tour) again, and read the articles available in the [Help Center](http://stackoverflow.com/help). – πάντα ῥεῖ Jun 07 '15 at 12:06

4 Answers4

3

Just read the error messages that you compiler gave you. The problem is that

‘cout’ was not declared in this scope

And the "suggested alternative" is std::cout. Same goes for string vs. std::string.

Note that generally, things belonging to the standard library need to be qualified with std:: to be found.

You also need to #include <string> to use std::string btw.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
3

The errors that you are getting are because the cout is not in the global namespace rather it is in std namespace.

Well instead of writing

using namespace std;

after #include <iostream> try using:

using std::cout;

since using first option is a bad practice. You can refer to Why is using namespace std is a bad practice. For benefits of using using std::cout refer Using std namespace

Also you can use std::cout everywhere if you don't want to use using std::cout.

Community
  • 1
  • 1
Rakholiya Jenish
  • 3,165
  • 18
  • 28
0

The compiler gives you the answer on line 7: Since you're not using the std namespace, you have to prepend std:: to your cout and cin calls.

Ghijs Kilani
  • 166
  • 1
  • 9
-2

Just add

using namespace std;

after #include <iostream>

Try this out.

witch3r
  • 87
  • 2
  • 9
  • 2
    I did not downvote because this is not wrong, but [please do not teach that to beginners](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Baum mit Augen Jun 07 '15 at 11:51
  • Could you explain why? I am a beginner too and I didn't quite catch your answer. Could you clarify? – witch3r Jun 07 '15 at 11:53
  • @RafizSalehinSejim IMHO, it is not a mistake and for small programs it can be completely fine to `using namespace std;`. However, one should be aware of potential problems. Actually, I saw this practice in many books and talks. It might be ok for small example snippets, but often they miss to mention that there are good reasons not to do it. – 463035818_is_not_an_ai Jun 07 '15 at 12:09
  • @RafizSalehinSejim `using namespace std;` is not the ultimate evil. If you e.g. need to present short code snippets (like when you give a talk), I personally still do not like it, but it won't cause harm. One should however understand the dangers, something beginners usually can't. Thus I say: Do not tell them to do this. – Baum mit Augen Jun 07 '15 at 19:47
  • @BaummitAugen Thanks a lot! I promise I won't – witch3r Jun 08 '15 at 04:42