Evaluate my code please?

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.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	cout << "Hello World!" << endl;
	_getch();
}
Last edited on
@HadMuffin

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>

using namespace 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
}


2nd example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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.

Hope all goes well :)
Could you explain the reason for the char a;? And the cin >> a; is what causes the CMD screen to pause so you can type things?

This is what I got now, is it any better? Haha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	char a;

	cout << "Hello World!" << endl;

	cin >> a;

	return 0;
}
> I like to break bad habits early
¿why do you suggest std::cin >> a; then?

http://www.cplusplus.com/forum/articles/7312/
> a console program should be run from the console --else once it terminates, the console should disappear.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 
My five euro cents.:)

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
	auto out = []( std::ostream &os, const std::string &s ) -> std::ostream & 
	{
		return ( os << s << std::endl );
	};

	out( std::cout, "Hello World" );
}
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. :)
Last edited on
Thanks all! Time to try to find a tutorial to make a calculator or maybe a game of pong? >:)
One more "Hello World".:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
 
template <typename T>
std::ostream & out( std::ostream &os, const T &item ) 
{ 
    return ( os << item << std::endl );
}
 
template <typename T, typename ...Args>
std::ostream & out( std::ostream &os, const T &first, Args ... args )
{
    os << first;
    return ( out( os, args ... ) );
}
 
int main()
{
 
        out( std::cout, "Hello World", "\nIt is me!" );
}
Topic archived. No new replies allowed.