absolute newbie: how to use cout ?

F

Francisco Ares

Hi, all

I'm trying to learn C++ using the "Thinking in C++" free book, but
right at the first "hello world" program (bellow) I got a lot of
warnings / errors.


// Saying Hello with C++
#include <iostream> // Stream declarations
using namespace std;

int main() {
cout << "Hello, World! I am "
<< 8 << " Today!" << endl;
}


Am I missing something? How do I tell gcc that this is a C++ program?

Thanks
Francisco
 
V

Victor Bazarov

Francisco Ares said:
I'm trying to learn C++ using the "Thinking in C++" free book, but
right at the first "hello world" program (bellow) I got a lot of
warnings / errors.


// Saying Hello with C++
#include <iostream> // Stream declarations
using namespace std;

int main() {
cout << "Hello, World! I am "
<< 8 << " Today!" << endl;
}


Am I missing something? How do I tell gcc that this is a C++ program?

This is really off-topic here (compiler options and command lines are
not part of C++ language), but try using g++ instead of gcc.

Victor
 
C

C++ nowbye

Ok try doing these things.

1. add .h to stream so it becomes #include <iostream.h>
2. remove the line using namespace std
3. simplify cout
better type cout << "Hello! World";
4. and always use return 0 before }
so the program would be

#include <iostream.h>

int main()
{
cout << "Hello! world";
return 0;
}
 
A

Alf P. Steinbach

"absolute newbie": all advice given by the troll is incorrect.


* C++ nowbye:
 
S

Sharad Kala

C++ nowbye said:
Ok try doing these things.

1. add .h to stream so it becomes #include <iostream.h>
2. remove the line using namespace std
3. simplify cout
better type cout << "Hello! World";
4. and always use return 0 before }
so the program would be

To OP - Please don't pay heed to the above given advice. You were doing
things correctly.
 
S

Sharad Kala

C++ nowbye said:
Ok try doing these things.

1. add .h to stream so it becomes #include <iostream.h>
2. remove the line using namespace std
3. simplify cout
better type cout << "Hello! World";
4. and always use return 0 before }
so the program would be

To OP - Please don't pay heed to the above given advice. You were doing
things correctly.
 
I

Ioannis Vranos

Francisco said:
Hi, all

I'm trying to learn C++ using the "Thinking in C++" free book, but
right at the first "hello world" program (bellow) I got a lot of
warnings / errors.


// Saying Hello with C++
#include <iostream> // Stream declarations
using namespace std;

int main() {
cout << "Hello, World! I am "
<< 8 << " Today!" << endl;
}


Am I missing something? How do I tell gcc that this is a C++ program?


g++ temp.cpp -o temp.exe


or better

g++ -std=c++98 -pedantic-errors -Wall temp.cpp -o temp.exe



The way I use it:

g++ -std=c++98 -pedantic-errors -Wall -fexpensive-optimizations -O3
-ffloat-store -mcpu=pentiumpro temp.cpp -o temp.exe






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
F

Francisco Ares

Victor Bazarov said:
This is really off-topic here (compiler options and command lines are
not part of C++ language), but try using g++ instead of gcc.

Victor

Thankyou!

Francisco
 
V

Victor Bazarov

Rolf said:
Ioannis Vranos wrote:




I'd add -W to the options.

Could you guys take it to gnu.g++.help, please? Compiler
options are off-topic here, in case you didn't know.
 
I

Ioannis Vranos

Rolf said:
Switches some other warnings on (don't remember which) that are not
enabled by -Wall (no, even though you could think so, -Wall doesn't
switch _all_ warnings on, not even in combination with -pedantic).


Because I couldn't find anything on the on line manual and do not
remember how to make g++ display all command line options (it is
something like g++ --help -v or whatever). If you remember how to do it,
please tell me how to do it.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
R

Rolf Magnus

Ioannis said:
What does it do?

Switches some other warnings on (don't remember which) that are not
enabled by -Wall (no, even though you could think so, -Wall doesn't
switch _all_ warnings on, not even in combination with -pedantic).
 
R

Rolf Magnus

I had a look, and -W seems to be renamed to -Wextra in newer versions.
On ttp://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Warning-Options.html, you
can read the details about which warnings are not switched on by -Wall
and which ones are added by -Wextra.
Because I couldn't find anything on the on line manual and do not
remember how to make g++ display all command line options (it is
something like g++ --help -v or whatever). If you remember how to do
it, please tell me how to do it.

Seems to be right. g++ --help -v prints out a huuuuuuge list of command
line options.
 
I

Ioannis Vranos

Rolf said:
I had a look, and -W seems to be renamed to -Wextra in newer versions.
On ttp://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Warning-Options.html, you
can read the details about which warnings are not switched on by -Wall
and which ones are added by -Wextra.


Ok, thanks for telling me.

Seems to be right. g++ --help -v prints out a huuuuuuge list of command
line options.



Well it seems I remembered well after all. On DJGPP it *works*, but on
MINGW it doesn't!


C:\>\mingw\bin\g++ --help -v
Reading specs from /mingw/bin/../lib/gcc-lib/mingw32/3.3.1/specs
Usage: g++ [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase
--help Display this information
--target-help Display target specific command line options
-dumpspecs Display all of the built in spec strings
-dumpversion Display the version of the compiler
-dumpmachine Display the compiler's target processor
-print-search-dirs Display the directories in the compiler's
search path

-print-libgcc-file-name Display the name of the compiler's companion
library
-print-file-name=<lib> Display the full path to library <lib>
-print-prog-name=<prog> Display the full path to compiler component
<prog>
-print-multi-directory Display the root directory for versions of
libgcc
-print-multi-lib Display the mapping between command line
options and
multiple library search directories
-print-multi-os-directory Display the relative path to OS libraries
-Wa,<options> Pass comma-separated <options> on to the
assembler
-Wp,<options> Pass comma-separated <options> on to the
preprocessor

-Wl,<options> Pass comma-separated <options> on to the linker
-Xlinker <arg> Pass <arg> on to the linker
-save-temps Do not delete intermediate files
-pipe Use pipes rather than intermediate files
-time Time the execution of each subprocess
-specs=<file> Override built-in specs with the contents of
<file>
-std=<standard> Assume that the input sources are for <standard>
-B <directory> Add <directory> to the compiler's search paths
-b <machine> Run gcc for target <machine>, if installed
-V <version> Run gcc version number <version>, if installed
-v Display the programs invoked by the compiler
-### Like -v but options quoted and commands not
executed
-E Preprocess only; do not compile, assemble or
link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o <file> Place the output into <file>
-x <language> Specify the language of the following input
files
Permissible languages include: c c++
assembler none
'none' means revert to the default behavior of
guessing the language based on the file's
extension

Options starting with -g, -f, -m, -O, -W, or --param are automatically
passed on to the various sub-processes invoked by g++. In order to pass
other options on to these processes the -W<letter> options must be used.
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls
--enable
-languages=c,c++,f77,objc,ada,java --disable-win32-registry
--disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x
--enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter
--enable-hash-synchroniz
ation
Thread model: win32
gcc version 3.3.1 (mingw special 20030804-1)
g++: no input files






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top