Is everything here okay? It works, and I think it looks pretty clean but let me know if i did it right and didn't use anything un necessary. Its the first time for me actually doing the Hello World without looking it up or cheating. :P Im pretty much a noob.
conio.h is not standard & not needed in this program. Just use std::cin instead of _getch(); there is a whole article about that in the documentation section.
I like to break bad habits early - so try this:
instead of line 4, put std:: before each STL thing, or specify a using statement for each STL thing:
1st example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <conio.h>usingnamespace std;int main()
{
char a;
std::cout << "Hello World!" << std::endl;
_getch();
std::cin >> a;
return 0; // I am old fashioned so I always do this
}
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char a;
cout << "Hello World!" << endl;
cin >> a;
return 0; // I am old fashioned so I always do this
}
The reason is that using the whole std namespace pollutes the global namespace (there are hundreds of things in std), causing conflicts in the naming of variables & functions - which is what namespaces is trying to avoid ! If you wrote a program that had distance, left, right, pair etc then you would have problems.
Some guys always use std:: regardless, but it is possible to do a mixture of styles - specify individual using statements for things used a lot, then do std:: for things you might not use as much like std::find say.
#include<iostream>
using std::cout; //print output
using std::cin; //read information from user
using std::endl; // end line -new line-
int main(){
//print Hello World + end line -new line-
cout<<"Hello World"<<endl;
//@ne555 "> a console program should be run from the console --else once it
//terminates, the console should disappear."
//but if you want to make a pause
cin.ignore();
return 0; //indicate successful termination
}//end main
I really like @eyenrique 's method, nice and short. :P Then again me being new I've got no clue how efficient it i (even though it doesnt really matter with a Hello World Im trying to find a method I can use for the rest of my c++ programming(and eventually game programming) days. :)