I'm trying to learn C++ using Borland C++ v5, but I can't even get a hello world program to work. Can anyone help me out on why or give me a beginner sample project to look at?
Printable View
I'm trying to learn C++ using Borland C++ v5, but I can't even get a hello world program to work. Can anyone help me out on why or give me a beginner sample project to look at?
Code:#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl
<< std::endl
<< "....." << std::endl
<< std::endl
<< "Goodbye, cruel world!" << std::endl
<< std::endl
<< "........." << std::endl
<< "BANG" << std::endl;
return 0;
}
you shouldn't need all these std::'s.
You do if you don't haveat the top of your code. By doing that you place yourself in the std namespace, which is the namespace all the functions in the standard library (which includes those in <iostream>) reside in. If you don't put the using statement in then you must explicitly qualify the scope of your calls to cout otherwise the compiler will be looking in the global namespace, which isn't where they are.Code:using namespace std;
Of course, if you use Microsoft Visual C++ and use this #include line:instead of using the C++ standard header with this line:Code:#include <iostream.h>
then you will get the Microsoft header file, which is non-standard. Using the MS header file either:Code:#include <iostream>
a) places you in the std namespace
or
b) places the standard functions, or an interface to them, in the global namespace
I don't know which it does, but frankly I don't like either. I'd much rather keep my code standard and portable than make it dependent on Microsoft's IDE and have it clean up my mess.
I take it you use the <iostream.h> header?
You will unless you did this (inside of main, before any other code):
OrCode:using std::cout; using std::endl;
Code:using namespace std;
To settle everything, this WILL work, provided you have your editor set up and everything (I use BTC++4.5):
Code:#include <iostream.h>
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
It won't, because you did not specify to use the std namespace.
Or if you write:Code:#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
Code:#include <iostream.h>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
Apparently, #include <iostream.h> will put you into the standard namespace, while <iostream> will not. As a side note, iostream.h using TC++ 3.0 is the same as using it in VC++ 6.0. It seems logical that iostream.h is an ANSI standard C++ library, similar to the STL container classes.
Z.
*sigh*
iostream is the standard, along with cstdlib, string, vector, etc. You shouldn't use the .h versions any more - they've been deprecated.
Therefore, Harry is correct :) Listen to that man, for he speaks sooth ;)