FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


The simplest program ever in C++ not working





Vitalist
Someone please explain me why this happening

Code:

#include <iostream>

int main()
{
   cout << "a";
}


The Error:
Code:
1>c:\users\user\documents\visual studio 2008\projects\greatworrior\greatworrior\rrrr.cpp(5) : error C2065: 'cout' : undeclared identifier


The error occurs when I am compiling.

Thank you very much.
flatliner
Try this! You forgot to use a name-space and also the function main should return an int.

Code:

#include <iostream>

using namespace std;

int main()
{
   cout << "a";

return 0;
}


OR

Code:

#include <iostream>

int main()
{
   std::cout << "a";

return 0;
}
tobenna
Vitalist wrote:
Someone please explain me why this happening

Code:

#include <iostream>

int main()
{
   cout << "a";
}


The Error:
Code:
1>c:\users\user\documents\visual studio 2008\projects\greatworrior\greatworrior\rrrr.cpp(5) : error C2065: 'cout' : undeclared identifier


The error occurs when I am compiling.

Thank you very much.


There are two ways too solve it one is by changing the line
Code:
cout << "a";

to
Code:
std::cout << "a";


or you could add the using statement at the top of your code so it would look like this

Code:

#include <iostream>
using std::cout;

int main()
{
   cout << "a";
}


Your code should be ok if you would use any of these two ways. Alright.
Fire Boar
As flatliner said, the function main should also return an integer value, since it is of the type int. But return 1 is generally used for errors: you should instead use return 0 by convention.
Raidation
It depends on whether he's using VC++ or not.
AftershockVibe
The EXIT_SUCCESS and EXIT_FAILURE constants are given in stdlib.h. This will define it correctly regardless of your compiler's implementation.
tobenna
Fire Boar wrote:
As flatliner said, the function main should also return an integer value, since it is of the type int. But return 1 is generally used for errors: you should instead use return 0 by convention.


Is it a must for a cpp program to return an integer value of either 1 or 0.
flatliner
Fire Boar wrote:
As flatliner said, the function main should also return an integer value, since it is of the type int. But return 1 is generally used for errors: you should instead use return 0 by convention.


Oh yes I do forget that sometime, bad habits.
Jamestf347
Code:
return 0;

would be the easiest, and simplest way.
dimedrol-tab
It is not good to let out all namespace std into a global space.
Serious programmers in serious companies use
Code:
std::cout << "Hello, world!\n";
or write
Code:
using std::cout; // ... and all data that they will use in their program
in beginning of code.
Related topics
My program
Me Thinks....
Decimal, Binary and Hexadecimal
[RESOLVED] 'localhost' database connection error!
Fortran Tutorials(77)
One Liners:: Funny and Somewhat Inspiring
C++ assignment help
Program i made not working for someone.
Game Maker
Programming
can winsock be used in c#
Deploying a C# program
Slight problem with C code
Basic C Programming Help?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.