|
-
Aug 7th, 2001, 09:39 AM
#1
Thread Starter
Stuck in the 80s
I might be retarded...
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?
-
Aug 7th, 2001, 09:50 AM
#2
Frenzied Member
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;
}
Harry.
"From one thing, know ten thousand things."
-
Aug 7th, 2001, 09:56 AM
#3
you shouldn't need all these std::'s.
-
Aug 7th, 2001, 10:16 AM
#4
Frenzied Member
You do if you don't have
Code:
using namespace std;
at 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.
Of course, if you use Microsoft Visual C++ and use this #include line:
Code:
#include <iostream.h>
instead of using the C++ standard header with this line:
Code:
#include <iostream>
then you will get the Microsoft header file, which is non-standard. Using the MS header file either:
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?
Harry.
"From one thing, know ten thousand things."
-
Aug 7th, 2001, 10:17 AM
#5
You will unless you did this (inside of main, before any other code):
Code:
using std::cout; using std::endl;
Or
Code:
using namespace std;
-
Aug 7th, 2001, 10:50 AM
#6
Member
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;
}
-
Aug 7th, 2001, 02:51 PM
#7
It won't, because you did not specify to use the std namespace.
Code:
#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
return 0;
}
Or if you write:
Code:
#include <iostream.h>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
-
Aug 7th, 2001, 03:18 PM
#8
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.
-
Aug 7th, 2001, 03:45 PM
#9
Monday Morning Lunatic
*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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|