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!