Sunday, August 2, 2009

Problem with a basic console code?

#include %26lt;iostream%26gt;


#include %26lt;stdlib.h%26gt;





int main(int argc, char *argv[])


{


cout %26lt;%26lt; "I pwn." %26lt;%26lt; endl;


system("PAUSE");


return 0;


}





i typed in this very simple code into dev C++ and it says 'cout' and 'endl' is undeclared. this is very basic and i dont know why it wont work

Problem with a basic console code?
You need to make the following changes.





#include %26lt;stdlib.h%26gt; --%26gt; #include %26lt;cstdlib%26gt;





Reason: Naming convention for standard library headers. All C library headers have a "c" for a prefix. And there is absolutely no ".h" in a standard header name.





cout %26lt;%26lt; "I pwn." %26lt;%26lt; endl; --%26gt; std::cout %26lt;%26lt; .... %26lt;%26lt; std::endl;





Reason: C++ standard library is in the std namespace. So you need to qualify everything in the namespace with std:: .
Reply:You are not defining your namespace. You need to include one of these:





using namespace std;





or





at the beginning of cout:





std::cout%26lt;%26lt;"I pwn...
Reply:Try this:





#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;





void main()


{


cout %26lt;%26lt; "I pwn." %26lt;%26lt; endl;


system("PAUSE");





}





You need to stay consistent when including header files ".h"...also you can use void main() and lose the return since you technically don't need it.





But, if you want to keep it the way you had it just put the ".h" after your iostream
Reply:Add this line:





using namespace std;





after #include %26lt;stdlib.h%26gt; and before int main(...)





cout, endl, and the rest of the C++ standard library are declared inside a namespace called "std", which stands for "standard".





Namespaces are just for organizational purposes, not something you really have to worry about most of the time. Just include that line and you should be good to go.
Reply:A better solution is to add this after your includes:





using namespace std;

deliver flowers

No comments:

Post a Comment