[Help] Compiling problem.

Started by
8 comments, last by MJP 16 years ago
I'am using the "Beginner C++ Game Programming" book, and i have some problems with the compiling on the 3rd chapter, a part of the code thad making the error: cout << count << " "; ++count; } cout << "\n\nCounting with nested for loops:\n"; const int ROWS = 5; const int COLUMNS = 3; for (int i = 0; i < ROWS; + + i) { for (int i = 0; j < COLUMNS; + + j) cout << i << "," << j << " "; cout << endl; } the error is on thad line: for (int i = 0; j < COLUMNS; + + j) im using Dev-C++, The Log: Compiler: Default compiler Executing g++.exe... g++.exe "D:\TwoLords\TutShit\Chapter2\counter.cpp" -o "D:\TwoLords\TutShit\Chapter2\counter.exe" -I"lib\gcc\mingw32\3.4.2\include" -I"include\c++\3.4.2\backward" -I"include\c++\3.4.2\mingw32" -I"include\c++\3.4.2" -I"include" -L"lib" D:\TwoLords\TutShit\Chapter2\counter.cpp: In function `int main()': D:\TwoLords\TutShit\Chapter2\counter.cpp:35: error: `j' undeclared (first use this function) D:\TwoLords\TutShit\Chapter2\counter.cpp:35: error: (Each undeclared identifier is reported only once for each function it appears in.) Execution terminated Thanks guys!
Advertisement
You probably want
for (int j = 0; j < COLUMNS; ++ j)
Quote:
for (int i = 0; i < ROWS; + + i)
{
for (int i = 0; j < COLUMNS; + + j)
cout << i << "," << j << " ";
cout << endl;
}


Look carefully at those two lines. You're declaring int i in both loops :)


for (int i = 0; i < ROWS; + + i){    for (int j = 0; j < COLUMNS; + + j)        cout << i << "," << j << " ";    cout << endl;}


should fix you right up.
(Please use [source]YOUR SOURCE HERE[/source] when posting code on the forums - it preserves formatting and adds syntax highlighting)

for (int i = 0; j < COLUMNS; ++j)

You're using 'j' here without telling the compiler to create 'j', so it has no idea what you're talking about. int i should read int j.

EDIT: Yeah, what the previous two guys said. ;-)
[TheUnbeliever]
I'am really confused about this one,
why the author of the book tells to type this code:
// Counter// Demonstrates for loops#include < iostream>using namespace std;int main(){   cout < "Counting forward:\n";   for (int i = 0; i < 10; + + i)     cout << i << " ";   cout << "\n\nCounting backward:\n";   for (int i = 9; i >= 0; - i)     cout << i << " ";cout << "\n\nCounting by fives:\n";for (int i = 0; i < = 50; i + =5)    cout << i << " ";cout << "\n\nCounting with null statements:\n";int count = 0;for (; count < 10;){     cout << count << " ";     ++count;}cout << "\n\nCounting with nested for loops:\n";const int ROWS = 5;const int COLUMNS = 3;for (int i = 0; i < ROWS; + + i){    for (int i = 0; j < COLUMNS; + + j)        cout << i << "," << j << " ";    cout << endl;}return 0;}


it has a lot of errors in it,
i guess he uses the same compiler,
so what wrong?
(maybe its because im using the .chm edition of the book because its ease'r for me, so the .chm is not written right?)

(this is the code thad he give's there, after i rewritted some of it with your fixes i still has an error)

[Edited by - IsseyMiyaki on March 24, 2008 7:56:56 PM]
Did you copy and paste that code from the .chm or did you re-type that out yourself?

Steven Yau
[Blog] [Portfolio]

Quote:Original post by yaustar
Did you copy and paste that code from the .chm or did you re-type that out yourself?


i copy past it.
maybe the .chm have's some errors...?

here is the Author words about it:
"If you're using an older compiler that doesn't fully implement the
current C++ standard, when you try to compile this program, you might get an error that says something like: error: ‘i’: redefinition; multiple initialization. Microsoft's old Visual C++ 6.0 is an IDE that will produce this type of error message.

The best solution is to use a modern, compliant compiler. Luckily for you, I've included on this book's CD-ROM a great IDE called Dev-C++ with a modern compiler. I highly recommend that you install and use this IDE when you're working on the programs in this book. After you install Dev-C++, check out Appendix A, "Creating Your First C++ Program," on the book's CD-ROM for a tutorial of how to get your programs running with Dev-C++.

If you must use your old compiler, you should declare any for loop counter variables just once for all for loops in a scope. I cover the topic of scopes in Chapter 5, "Functions: Mad Lib."
Quote:The best solution is to use a modern, compliant compiler. Luckily for you, I've included on this book's CD-ROM a great IDE called Dev-C++ with a modern compiler.

I can't believe this. Don't use Dev-C++. It is anything but "modern" and isn't being supported or developed anymore, I don't believe. I mean, it is ok for now since you are already using it, but when you get the chance you ought to get a new IDE, such as Code::Blocks or Microsoft Visual C++ Express Edition 2008 (both free).

Quote:
for (int i = 0; i < 10; + + i)

Honestly, I'm not sure if the space between the "+ +" is valid...I'd get rid of that.

Quote:
for (int i = 9; i >= 0; - i)

This should read "for(int i =9; i >= 0; --i)". Note that it is "--", just like "++". The "--" and "++" decrement or increment the variable they are put in front of, so just having one minus sign doesn't actually do what you want in this context.

Quote:
int count = 0;
for (; count < 10;)
{

cout << count << " ";
++count;
}

Do you know anything about while loops, or just for loops? This would be much more readable as a while loop, though it is valid as is. If you don't know while loops yet, it is most likely in the next chapter...
Thanks i have managed to fix it with your help =]

P.S can i can have Microsoft Visual C++ Express Edition 2008 link, because i have found only 2005 and its not free i guess because i have only 30 trial days.

thanks.
Here ya go.

This topic is closed to new replies.

Advertisement