Both my bf and I are settling down to teach ourselves C++, so he bought a book that looked really helpful.
I'm a few exercises into the second chapter, sitting currently on one that teaches about setting precedence.
I typed in the code precisely the way it is shown in the book, like so:
#include <iostream>
using namespace std;
int main()
{
num = 1 + 4 * 3;
cout << endl << "Default order:" << num << endl;
num = ( 1 + 4 ) * 3;
cout << "Forced order:" << num << endl << endl;
num = 7 - 4 + 2;
cout << "Default direction:" << num << endl;
num = 7 - ( 4 + 2 );
cout << "Forced direction:" << num << endl;
return 0;
}
All well and good, right? Well, not exactly. When I went to compile the code (using the GNU compiler in Ubuntu LInux), I got the following error in my terminal screen:
"7: error: ‘num’ was not declared in this scope" (the error is on line 7)
There is a warning note in the margin of my book that says not to rely on default precedence as it may vary between compilers, and to always use parentheses to clarify expressions.
So I tried doing just that...putting parentheses on the calculations that didn't originally have them, where I know the parentheses might go (multiplication before addition, subtraction, etc)...and recompiled, but I still got the same error.
I checked all my braces, brackets, semicolons, everything. I even removed the parentheses and recompiled again. Same error result.
I've sifted through at LEAST five pages of Google results, and nothing even matches my issue. So, what gives? Perhaps the book exercise is flawed...or there's something my compiler doesn't like about this specific exercise.
BB,
Kat ^.^

Start a new topic
Add Reply




MultiQuote




| 


