Thread: Help with for loop, please.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Help with for loop, please.

    I have a cpp program that I'm having trouble with; particularly in the for loop.

    Code:
    void myInit() {
    
            glClearColor(0, 0, 0, 0);
            glShadeModel(GL_SMOOTH);
    
            //init aliens, took me waaaay too long to figure this out
            head_alien = NULL;
    /*      for (int i = 0; i < row_aliens; i++) {
                    for (int j = 0; j < col_aliens; j++) {
    
                            aliens current;
                            current.x = al_init_x + j * (alien_size + alien_spacing);
                            current.y = al_init_y + i * (alien_size + alien_spacing);
                            current.next = head_alien;
                            head_alien = &current;
    
                    }
            }
    */
            head_bullet = NULL;
    
            cout << "\nNothing yet...\n";
            cout.flush();
    
    }       //end myInit
    I narrowed down the problem to that commented out for loop. If I left it in, I get errors like "error: expected primary-expression before '=' token" for the first for loop amongst others. Am I treating for loops incorrectly for c++? It worked this way in a c program I had. Or is the problem somewhere else entirely?

    Also, all the head_alien, row_aliens, col_aliens, etc are either #define or global variables.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That depends on which line has the error in it. But I like guessing, so here I go: you did this
    Code:
    #define row_aliens = 10
    instead of
    Code:
    #define row_aliens 10

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Quote Originally Posted by someoney3000
    Quote Originally Posted by tabstop View Post
    That depends on which line has the error in it. But I like guessing, so here I go: you did this
    Code:
    #define row_aliens = 10
    instead of
    Code:
    #define row_aliens 10
    Wow, you're absolutely right! I did do that wrong, thanks!

    So I went back and changed all of them, but I'm still getting the same errors.

    Code:
    g++ -lglut galaxian.cpp 
    galaxian.cpp: In function 'void myInit()':
    galaxian.cpp:78: error: expected primary-expression before ';' token
    galaxian.cpp:78: error: expected ')' before ';' token
    galaxian.cpp:78: error: name lookup of 'i' changed for ISO 'for' scoping
    galaxian.cpp:78: note: (if you use '-fpermissive' G++ will accept your code)
    galaxian.cpp:78: error: expected ';' before ')' token
    galaxian.cpp:268: error: expected '}' at end of input
    Judging from the errors in my very limited experience, I was expecting that I was missing a } or ) somewhere. But I simply commented out the for loop, then all the error goes away. That's why I think it's somewhere in the for loop. Is anyone seeing an error?

    Ah, right, line 78 is the beginning of that for loop.
    Ah, nevermind. I'm a retard.

    You're right that I did the #define incorrectly

    I did something like this:

    Code:
    #define row_aliens 10;
    That semicolon is at fault.

    Thanks! It seems I owe you twice in one day!
    Last edited by someoney3000; 10-14-2009 at 01:05 PM. Reason: clarification; I'm a retard

Popular pages Recent additions subscribe to a feed