Sign in to follow this  
IsseyMiyaki

[Help] Compiling problem.

Recommended Posts

IsseyMiyaki    122
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!

Share this post


Link to post
Share on other sites
TripleOption    122
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.

Share this post


Link to post
Share on other sites
TheUnbeliever    963
(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. ;-)

Share this post


Link to post
Share on other sites
IsseyMiyaki    122
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]

Share this post


Link to post
Share on other sites
IsseyMiyaki    122
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."

Share this post


Link to post
Share on other sites
bschneid    126
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...

Share this post


Link to post
Share on other sites
IsseyMiyaki    122
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this