0

I just wanted to ask about this tutorial.

I don't get it. The code they write is like this:

intmain() {
    cout << "Hello World!\n";
    return0;
}

Now, I'm really really new to c++, but surely it should be:

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

Or is there some fancy way of writing c++ that I don't understand? The code written in this tutorial won't compile for me.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
Chud37
  • 4,907
  • 13
  • 64
  • 116

4 Answers4

11

You are correct; that "tutorial" is chock full of formatting mistakes.

Many, many, many C++ tutorials are wrong. Don't trust the word of some randomer on the internet; use a proper, peer-reviewed book.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

They clearly had problems with spaces and quotes when they copy-pasted their code.

As a result, their code is not copy-paste ready.

I believe it should be:

#include <iostream>
using std::cout;
int main()
{
    cout << "Hello world!\n";
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Yes it's weird really, because theres comments at the bottom of people saying "Thanks! Works great!" sort of thing. I don't get it. – Chud37 May 19 '16 at 18:18
  • Maybe the formatting was changed by someone who does not know `c++` after the article was published. Although that does not explain the missing std:: – drescherjm May 19 '16 at 18:20
1

Or is there some fancy way of writing c++ that I don't understand?

Well, there still could be nasty things presumed like using c-preprocessor macros1:

#define intmain int main
#define return0 return 0

But I have to agree with @Lightness, just don't trust random sources for learning.

A good one for c++ I use at an almost daily basis for getting a reliable reference is cppreference.com.


1)I'd escalate seeing such to my boss as blatant incompetence, and recommend to fire the author that wrote such code.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Well, obviously, you need to have spaces between return 0; and int main().

Now, lemme go ahead and explain the std:: for you. std is a 'namespace'. Basically, its used to group similar code together, to avoid name collisions. The 'standard' (std) namespace includes well, the basic C++ features. In new(ish) versions of C++, you need to specify the namespace you take your functions from.

You could, do "using namespace std;" at the beginning, or specify the namespace individually, like in your corrected code, for each standard function you use.

MerajA
  • 194
  • 5
  • 19
  • 2
    You should never have `using namespace std;` [***Link***](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – abelenky May 19 '16 at 17:32
  • @abelenky True, but it doesn't really matter for trivial pieces of code. – MerajA May 19 '16 at 17:34
  • 3
    It matters **more** for trivial pieces of code. It brings in the massive `std::` namespace for your code to use 1 or 2 items. When another developer reads the code, it tells them *nothing* new. (all code uses `std::`, but it still doesn't tell them what parts get used) – abelenky May 19 '16 at 17:35
  • 5
    imho `using namespace std;` is one the worst habit of tutorial writers. It merely saves you from writing 4 letters (which actually I personally see as a downside) and makes beginners believe that it would be good practice – 463035818_is_not_an_ai May 19 '16 at 17:44