Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Super newbie question...

Status
Not open for further replies.
Feb 14, 2002
88
0
0
JP
Yeah, I know, this is really lame, but you have to start somewhere. I really only know bash/ksh and perl for the most part, but I'm the only one in my company that doesn't know C++, so I figured I'd better start.

I've been digging around on the net and have found some good information, but I think the way C/C++ works (different compilers, etc.) might be a bit troublesome for the from-scratch starter.

Anyways, on w/ my problems.
1: To learn the code, I'm just trying samples out in VI. The first, and most annoying, problem was that when I try to type a comment w/ "//" every succeeding line automatically starts with "//". Not sure what's up w/ that.

2: When I compile my &quot;hello world&quot; code w/ the g++ compiler (g++ hiworld.cpp -o hiworld) I get the warning about one or more deprecated or antiquated headers. The only header I've got is <iostream.h> so I assume that's the problem.

The code looks like:

#include <iostream.h>

int main ()
{
cout << &quot;Hello World!&quot;;
return 0;
}

It compiles okay, other than the warning, but when I execute it - it doesn't print out the Hello World on the terminal.

Doing something obviously wrong?

Also - any links to Learn C++ tuturial pages for the absolute beginner would be appreciated.
 
The &quot;.h&quot; should be omitted from all C++ standard headers as that is a deprecated usage. Try:

#include <iostream>

Not sure why you're not seeing the output, but in addition to the header name change, try adding a newline to it:

cout << &quot;Hello World!&quot; << endl;

The automatic insertion of another &quot;//&quot; is just Vi making things easy for you, but I guess you find it inconvenient :) I'm sure this is customizable, but I don't know off-hand where you need to make the change. Maybe someone else knows. If you don't get an answer here, try the &quot;General UNIX Questions&quot; forum.

If you really want to learn C++ and learn it right, you're much, much better off with a good book than a web tutorial. Accelerated C++ by Koenig & Moo and the C++ Programming Language by Stroustrup are good.
 
Wow - thanks for the fast reply!
I had actually found another web tutorial that used iostream instead of iostream.h

Also - your endl was the key. I assume this is similar to Perl's &quot;\n&quot; at the end of a line. Has caused some headaches in the past.

Thanks for the book recommendations. I'll check them out. I'm always cautious about buying computer books I don't know about, especially w/ a multi-platform language. W/ Perl, it's almost all Unix unless otherwise noted. I don't know enough about C/C++ to know what to look for. I'm hoping they don't write things around different compiles and whatnot. I'll definitely swing by Amazon and check it out.
 
Yeah, that comment thig is pretty annoying, isn't it?

I assume you're using Vim and not regular vi ('cuz regular vi usually doesn't do that). Start up Vim and use:

Code:
:help 'comments'

and

Code:
:help 'format-comments'

to get an idea of what Vim is thinking. Then edit (or create) a file in your home directory called &quot;.vimrc&quot; (note the dot) to contain the comment string that makes Vim behave the way you think it should. If you don't feel like reading, I think you can just put this line in your .vimrc:

Code:
set comments &quot;&quot;

and it'll just ignore all comments.


I'm not sure, but I think the thing with the output not showing up unless you have a newline is a &quot;feature&quot; of the bash shell. Scared the crap out of me when I switched to bash and discovered I'd apparently forgotten how to write &quot;Hello, world.&quot;
 
Syntax corrections:

Use

Code:
:help format-comments

without the single quotes or it won't work.


The line that goes into .vimrc has to look like:

Code:
set comments=&quot;&quot;


Sorry 'bout that.
 
Well I'll be... I am using VIM. Even though I invoke it w/ VI.

As for bash... I'm slowly starting to hate it.
I liked using ksh back on HP-UX at my last job, just sticking &quot;set -o vi&quot; in the profile and never retyping anything. I know there's a way around it in bash, but I've just been lazy.

And yes, the &quot;format-comments&quot; thing is working... I'll play around with that. It seems the indentation thing is acting silly as well. :(

Thanks for the help!
 
(Going off topic here).

If you mean command-line editing, bash lets you cycle through your command history with the up and down arrow keys. I feel that's far superior to ksh-style command line editing (which I used to really like).

If you like the vi editing style better than the bash default (emacs?), I took a quick look at the bash man page and it looks like bash supports that; just put &quot;set -o vi&quot; in your .bash_profile if you have one; otherwise keep it in your .profile. (Bash doesn't look at .profile if it finds .bash_profile first).
 
bash uses the readline library, which was created for bash, but is now used in a lot of applications.

//Daniel
 
(staying a bit off topic...sorry)

I know how to do the &quot;set -o vi&quot; but putting it in the .bash_profile doesn't seem to work.... well, it didn't a while ago, but does now that I tried it again. Hmm.... I wonder if I was doing something wrong. Kinda made me hate bash for a while. :)

Someone on another board told me to put it in vi files, which didn't cut it.

Anyways, things are looking up for me I suppose. :)
 
You have to know that streams in unix have restricted application
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top