Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-03-2003, 03:40 PM   #1
Member (12 bit)
 
Markoman01027's Avatar
 
Join Date: Jul 2002
Location: Easthampton, Massachusetts
Posts: 2,633
Hello World Code

Hey Guys,

First day coding in my C++ class at high school. The teacher gave us a sample code of the Hello World code that she typed up, and it was wrong. So I went online to reference the code and I found many different ways to write it. Just wondering if there is a proper way of using it.

My second question is, do I have to use the "\n" command after each Cout statement? What about "endl" command. Do I have to use that too after each cout statement? or can I simply just use ";" for the end of the line.

I have two links here, showing you different way to code the Hello World program.

Tell me which way is the proper way. I used this:

"cout <<"\n hello world!\n"; and my teacher said "That is old code" I don't think it matters, aslong as you get the program to do what you intended it to do.

I found different ways of writing the code. One way of only using the << Brackets on the left side and not on the right side ..Example:

"cout <<"\n hello world!\n";"

And I also heard that you have to use it at the right side aswell.. Example:

""cout <<"\n hello world!\n << ";"

The extra " is so the website doesn't read the code or whatever.

Code 1:
""#include
using namespace std;

int main () {
cout << "Hello World" << endl;

return 0;

}""

Question for code 1 : Do I HAVE to use Namespace std? Do I HAVE to use endl?

Code 2:
hallo.cpp

""#include // ::std::cout #include // <<
int main(){ ::std::cout << "Hallo!" << '\n'; }""


Sorry for making this a long thread and a little confusing. But if you guys can answer my questions, that'd be great because it will clear up the confusion of which way is the best way to program.

Thanks!
Markoman01027 is offline   Reply With Quote
Old 09-03-2003, 03:46 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
\n is the escape code for a new line. endl is pretty much the same thing. Both simply hit "enter" after you output something. For example, if you did this:

cout<<"Hello world!";
cout<<"Goodbye world!";

the output would run together on one line like this (as with any programming, try it for yourself and see):

Hello world!Goodbye world!

Putting the \n or endl at the end simply adds a hard return, carriage return, whatever you want to call it:

cout<<"Hello world!"<<endl;
cout<<"Goodbye world!";

outputs what you probably intend:

Hello world!
Goodbye world!

As for the using namespace std;, you probably don't HAVE to include it. But it is proper for ANSI C++. If you want to get more information about what it does, go here
doctorgonzo is offline   Reply With Quote
Old 09-03-2003, 03:50 PM   #3
Member (12 bit)
 
Markoman01027's Avatar
 
Join Date: Jul 2002
Location: Easthampton, Massachusetts
Posts: 2,633
Makes perfect sense DoctorGonzo. Thank you very much for your help!
Markoman01027 is offline   Reply With Quote
Old 09-03-2003, 04:25 PM   #4
The Boneshaker
 
nocturnx's Avatar
 
Join Date: Jun 2003
Location: Ohio
Posts: 1,266
Send a message via AIM to nocturnx
Oh the Hello World code.....brings back memories.......sigh
Thats kinda funny, your school must be using the same book i used in college or do all books start off with the hello world code.....just wait till you get to the fun stuff, then its all worth it.
__________________
Leave it to me as I find a way to be
Consider me a satellite, forever orbiting
I knew all the rules, but the rules did not know me
Guaranteed

---Eddie Vedder, “Guaranteed”.
Rest in Peace, Evan.
2.11.71 - 9.8.08
nocturnx is offline   Reply With Quote
Old 09-03-2003, 07:19 PM   #5
Member (11 bit)
 
james8547's Avatar
 
Join Date: Aug 2003
Location: NJ
Posts: 1,099
Yes Hello World

First thing I ever coded in any languge I studied (C++, Assembly, VB) was Hello World. I still have a copy of the program. Too bad some virus appended itself to it and I can't get it out. It's archived in a cd.
__________________
P4 2.6C @ 3.12 || ASUS P4C800-E Dlx || Antec SOHO File Server w/ 5 case fans || Antec TruePower 430w PSU || 2 x 512MB Crucial PC3200 DDR || 280GB of total storage 7200rpm ATA100 8mb cache || LiteOn CDRW 52x32x52 || LiteOn DVD+/-RW SOHW-812S || WinXP Pro || Solarism 15" TFT LCD || 500VA TrippLite UPS || Logitech MX-700 Duo || ATI 9800 AIW || Sennheiser HD-555
james8547 is offline   Reply With Quote
Old 09-03-2003, 09:01 PM   #6
Member (12 bit)
 
Markoman01027's Avatar
 
Join Date: Jul 2002
Location: Easthampton, Massachusetts
Posts: 2,633
I don't think we even use books in my high schoo.. She just hands us code and tells us to code it into our compiler. I pretty much teach myself in that class. Not sure if her methods of teaching are any useful for me.
Markoman01027 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:05 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2