newbie #include<...> question

rako

Registered
Hello - I am new to C++ and going through an intro to C++ book. I am attempting to run the following:

#include <iostream.h>

int main() {
std::cout << "Please work, please";
return 0;
}

I have also tried the following:

#include <iostream.h>

int main() {
cout << "Please work, please";
return 0;
}

I can compile and run a program with NO include# with just an empty main() funtion, so I am assuming I don't know how to incorporate the standard libraries.

TIA:confused:
 

rako

Registered
I just noticed that posting my question clipped off my #include. I am trying to use the iostream header file.

Thanks
 

rharder

Do not read this sign.
Do you mean #include "streamio.h" ?

-Rob
 

rako

Registered
Hi - In my book it says to include iostream.h.

Sorry if I am missing something really obvious.

btw, the code works in project builder (C++ Tool) but i would like to stay in the terminal.
 

blb

`'
The following C++ code compiles and works without error for me:

#include &lt;iostream.h&gt;

int main( void )
{
cout << "Just a C++ test" << endl;

return 0;
}

(into a file test.cpp), then to compile,

$ c++ -o test test.cpp

and run,

$ ./test
Just a C++ test
 

MikeySquid

Registered
On OSX using g++ (gcc) you have to put an endl; after every write in order to flush the buffers. Your iostream.h is fine. you just have to do an endl. You can copy and paste the code from you book into your program, just terminate all output, whether to a file or stdio, with endl; I did some fairly major programs for my data structures class in school using this technique and everything worked fine.

btw: stdio is for c and works with printf. iostream.h is c++ and works with cout.
 

kilowatt

mach-o mach-o man
LOL

It doesn't surprise me about the silly ness in my stdio post... it should clearly point the finger at my BASIC, Perl, and Python skills....... or inexperience, if you want to be more specific.

Would anyone mind clue ing me in as to the differences between c++ and c?

Most threads avoid the topic completely.

Thanks!
 

MikeySquid

Registered
Every one learns this somewhere along the line. There is no fundamental difference between c and c++. C++ is an enhancement to c. I'm not that knowledgeable about c so i apologize in advance for my errors.

You want to output something to the screen, so you write "cout<<"Hello World";". When you compile the code the compiler "parses" the line and knows that the first "word" is an instruction because that is the convention. The compiler then looks to it's internal library of instructions on how to do a cout. The definition of cout is in the iostream.h file. that is why you have to include it. If you do not #include iostream.h the compiler doesn't know what cout means. the definition of printf is in the stdio.h file. One reason that they redefined output in c++ is so that you can use the same "definition" and the same syntax no matter the output method. In other words to output to the screen "cout<<"Hello world"; to output to a file "AFile<<"Hello World"; They just gave outputting a common user (programmer) experience which simplifies it. That is all. Hope this helps and good luck.
 

mkwan

Tech
this may look like a stupid question, but how do i compile and run the c++ source code through terminal?



mkwan:(

iBook 500
 
Top