|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (12 bit)
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 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! |
|
|
|
|
|
#2 |
|
Professional gadfly
|
\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 |
|
|
|
|
|
#3 |
|
Member (12 bit)
Join Date: Jul 2002
Location: Easthampton, Massachusetts
Posts: 2,633
|
Makes perfect sense DoctorGonzo. Thank you very much for your help!
|
|
|
|
|
|
#4 |
|
The Boneshaker
|
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 |
|
|
|
|
|
#5 |
|
Member (11 bit)
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 |
|
|
|
|
|
#6 |
|
Member (12 bit)
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.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|