Hello World

Hi. I use the book ''Programming Principles and Practice using C++'' by the creator of C++ Bjarne Stroustrup
The first task was the classic 'Hello world'. In the book there was a code looking like this:

1
2
3
4
5
6
7
  #include 'std_lib_facilities.h'

int main()
{
	cout << 'Hello World!\n';
	return 0;
}


I tried typing that code, but it didnt work. I then went to youtube and fount another code. That code looked like this;

1
2
3
4
5
6
7
#include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    std::cin.get();
    return 0;
}


This code worked perfectly fine. I tried to do some modifications on the first code to make it work aswell. i came up with this;

1
2
3
4
5
6
7
#include <iostream>

int main()
{
	std::cout << "hello world!\n";
	return 0;
}


So basically my question is: Why do this work and not the first? i want learn what everything means so i know next time im writing code. If someone can give a simple and understandable explanation it would be perfect! Thanks.

closed account (iAk3T05o)
Remove the single quotes and try again (newbie too, so don't know if it'll work)
Well basically you needed to download the std_lib_facilities.h header because you didnt had it on your computer.Here is the link : https://www.mediafire.com/?c3t2a8s8pdxau7j
P.S : That header is really just a buch of other headers putted togheter and some extra stuff.

After you downloaded the header just put it in your compiler include folder.

For example : C:\Program Files\CodeBlocks\MinGW\include

or

C:\Program Files\Microsoft Visual Studio 10.0\VC\include

It depends on what complire/ide youve got.

Now you shouldnt receieve the "no such file or directory error".

The code should look like this now:
1
2
3
4
5
6
7
#include "std_lib_facilities.h"

int main()
{
	cout << "Hello World!\n";
	return 0;
}


As you can see i replaced the ' with " because ' is used for only one character and " is used for more than one character.


Last edited on
@Nathan2222 It does not work.
@SorinAlex Thanks. It worked.
Happy to help :)
closed account (j3Rz8vqX)
Well, for starters:

There is a difference between:
First code:
 
cout << 'Hello World!\n';


and

Second code:
 
cout << "Hello World!\n";


Single quotes mean character.
Double quotes mean string.

Character can consist of only one character, ie: 'h' or '5' or '$'

String can consist of multiple characters, ie: "Hello World" or "123456" or "abc456"

cout reference: http://www.cplusplus.com/reference/iostream/cout/
Not sure if the lib is native...
 
#include 'std_lib_facilities.h' 


if the lib is valid, it should be implemented like this:
 
#include "std_lib_facilities.h" 


Normally folks using c++ use
 
#include <iostream> 



The cin.get() isn't necessary but a good way to see your console window open if you're running from an IDE.
closed account (Dy7SLyTq)
well just to do a bit of exanding... a string is one or more characters. ie "a" is a valid string. in c++ they are different data types. in python its all a string.
std_lib_facilities.h is probably something he wrote because its not in the stl. you only use <> in includings when its on the search path. if you wrote it and want to be lazy, then you need to use quotes ie " and cin.get() only works if the buffer is clear
I dont know who has written this blog, but Its simple to read.Although its not complete yet.Just give it a look
http://www.programming-gum.blogspot.com
Last edited on
Topic archived. No new replies allowed.