Sams Teach Yourself - codes from book doesn't work.

Hello.
I've just started my c++ learning. I've bought book called "Sams Teach Yourself C++" 8th edition and codes attached to book by publisher... doesn't work.
It is a bit frustrating - how I suppose to learn anything, if codes provided by author are wrong?
After forum search, I've found opinion that the book is "trash" :-)
But I decided to give it another try and ask You guys what might be wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
using namespace std;

int main()
{
   auto coinFlippedHeads = true;
   auto largeNumber = 2500000000000;

   cout << "coinFlippedHeads = " << coinFlippedHeads;
   cout << " , sizeof(coinFlippedHeads) = " << sizeof(coinFlippedHeads) << endl;
   cout << "largeNumber = " << largeNumber;
   cout << " , sizeof(largeNumber) = " << sizeof(largeNumber) << endl;

   return 0;
}


This code produces faults
error: ISO c++ forbids declaration of 'coinFlippedHeads' with no type
error: ISO c++ forbids declaration of 'largeNumber' with no type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
constexpr double GetPi() { return 22.0 / 7; }
constexpr double TwicePi() { return 2 * GetPi(); }

int main()
{
   using namespace std;
   const double pi = 22.0 / 7;

   cout << "constant pi contains value " << pi << endl;
   cout << "constexpr GetPi() returns value " << GetPi() << endl;
   cout << "constexpr TwicePi() returns value " << TwicePi() << endl;

   return 0;
}


This one produces faults
error: expected constructor, destructor, or type conversion before 'double'
'GetPi' was not declared in this scope
'TwicePi' was not declared in this scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
constexpr int Square(int number) { return number*number; }

int main()
{
   const int ARRAY_LENGTH = 5;

   // Array of 5 integers, initialized to five
   int myNumbers [ARRAY_LENGTH] = {5, 10, 0, -101, 20};

   // Note - using a constexpr
   int moreNumbers[Square(ARRAY_LENGTH)] = {25};

   cout << "Enter index of the element to be changed: ";
   int elementIndex = 0;
   cin >> elementIndex;

   cout << "Enter new value: ";
   int newValue = 0;
   cin >> newValue;

   myNumbers[elementIndex] = newValue;
   moreNumbers[elementIndex] = newValue;

   cout << "Element " << elementIndex << " in array myNumbers is: ";
   cout << myNumbers[elementIndex] << endl;

   cout << "Element " << elementIndex << " in array moreNumbers is: ";
   cout << moreNumbers[elementIndex] << endl;

   return 0;
}


This gives me:
error: expected constructor, destructor, or type conversion before 'int'
error: 'Square' was not declared in this scope
error: 'moreNumbers' was not declared in this scope

I haven't done even 10% of the book, and there alredy 3 faulty codes.
What do you Think? Bin the book?
I'm programming in CodeBlocks 16.01.

Thanks!
The book _is_ trash, but your compiler also needs help.

Make sure you are using the latest version of GCC/MSVC/Clang++/whatever it is you are using and that it supports the C++14 standard.

For GCC, you have to tell the compiler explicitly, every time, to use C++14. If you are using an IDE, go to your IDE's compiler settings and find the checkbox to turn on "Compile with C++14 Standard" or something like that. At the command prompt:

    g++ -std=c++14

If you're using Turbo C++ or anything like that, that is an ancient, pre-standard C++ that you'll have to put up with until the University finally gets smart and stops telling students to use it. In your own time at home, get a modern compiler.

Hope this helps.
Thank you Duthomhas.
This is very usefull.

At first I've started on-line course of c++ where guy recommended CodeBlocks IDE. But the course is couple of years old already, so this is probably the problem.

What IDE would you recommend?

Is there any other book/course you would recommend for total beginner, who has no coding experience at all?

Thanks.
C::B is an excellent IDE.

I haven't looked at what it comes with lately. You may need to update the compiler. But, like I said, you need to tell C::B to frob GCC with the C++14 flag.

https://zepix.wordpress.com/2015/07/01/how-to-enable-c14-in-codeblocks/
It does have c++14, tough i personally prefer using Atom as my IDE, looks nicer.
Topic archived. No new replies allowed.