# C++ Tutorial problem with part 4: loops

c++ sprites

8 replies to this topic

### #1RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 10:42 AM

Hi everyone,

I am trying to finish every C++ that IGAD posted here on this website.
I currently am in tutorial part 4: loops

It's going okay but I've ran into a problem with the assignment..
This is the assignment:
Create a nestedfor loop (a loop inside a loop) that draws 10 lines of 13 tank

This is what I had before I started the assigment (so after I completed all the steps of the tutorial)

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009
#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"
using namespace Tmpl8;
void Game::Init()
{
// put your initialization code here; will be executed once
}
Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );
void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
or( int i=0; i < 13; i++ )
{
theSprite.SetFrame( i );
theSprite.Draw( i*50, 0, m_Screen );
}
}


So the assignment tells me to create a second loop within the current loop: for( int i=0; i < 13; i++ )
I have no idea how to do this.. I have tried several things such as trying to implement a second variable into the loop, which (i think?) should look as the following code:
// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009
#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"
using namespace Tmpl8;[/size][/font]
void Game::Init()
{
// put your initialization code here; will be executed once
}
Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );
void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
for( int i=0; int u=0; i < 13; u < 10; i++; u++; )
{
theSprite.SetFrame( i );
theSprite.Draw( i*50, u*50, m_Screen );
}
}


As you can see, I have added the variable u in the loop and changed the 0 in theSpite.Draw into u*50.. yet I get a build error.

Help much appreciated
Cheers !

### #2RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 10:44 AM

I have the feeling that it it's probably a very easy fix but that I am overlooking it..

### #3RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 10:58 AM

Before I added the variable u, it worked and I saw 1 row of 13 tanks.

this is my error log:

Compiling...
cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
game.cpp
c:\myprogrammingprojects\loops\game.cpp(24) : error C2146: syntax error : missing ')' before identifier 'u'
c:\myprogrammingprojects\loops\game.cpp(24) : warning C4552: '<' : operator has no effect; expected operator with side-effect
c:\myprogrammingprojects\loops\game.cpp(24) : error C2059: syntax error : ';'
c:\myprogrammingprojects\loops\game.cpp(24) : warning C4552: '<' : operator has no effect; expected operator with side-effect
c:\myprogrammingprojects\loops\game.cpp(24) : error C2065: 'i' : undeclared identifier
c:\myprogrammingprojects\loops\game.cpp(24) : error C2065: 'u' : undeclared identifier
c:\myprogrammingprojects\loops\game.cpp(24) : error C2059: syntax error : ')'
c:\myprogrammingprojects\loops\game.cpp(26) : error C2065: 'i' : undeclared identifier
c:\myprogrammingprojects\loops\game.cpp(27) : error C2065: 'i' : undeclared identifier
c:\myprogrammingprojects\loops\game.cpp(27) : error C2065: 'u' : undeclared identifier

### #4roel

Senior Member

• Members
• 698 posts

Posted 17 March 2012 - 11:13 AM

That's not how for loops work. It has always the form:
for(initializer;condition;expression) { code }

What the probably mean is that you create another for-loop in the other for-loop
for(int u=...)
{
for(int i...)
{
}
}


### #5RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 11:53 AM

Ah alright thank you, I'll give it a shot.

The first one is okay, right? :
for( int i=0; i < 13; i++ )

I mean, this is a valid for loop?

Thanks for your help! I thought I had to fill the u in the for i loop..

Yet I do not know how to implement what you've just told me in my code.. where do i put the theSprite.Draw etc? It's confusing because now there are two loops !

### #6RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 11:58 AM

This is my new code ( the part that we're talking about ), i tried to implement what you told me.

for( int u=0; u < 10; u++ )
{
for( int i=0; i < 13; i++ )
theSprite.SetFrame( i );
theSprite.Draw( i*50, 0, m_Screen );
}
}


I get a build error and it tells me that 'i' is an undeclared identifier

### #7RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 12:06 PM

Now I have this but it still doesn't work..
for( int u=0; u < 10; u++ )
{
for( int i=0; i < 13; i++ )}
{
theSprite.SetFrame( i );
theSprite.Draw( i*50, 0, m_Screen );



### #8RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 12:10 PM

even this doesn't work..

{
for( int u=0; u < 10; u++ )
{
for( int i=0; i < 13; i++ )
}
theSprite.SetFrame( i );
theSprite.Draw( i*50, 0, m_Screen );
}



### #9RichardvBrunschot

Member

• Members
• 11 posts

Posted 17 March 2012 - 01:26 PM

Nevermind I got it

This is my code now, ( the part that my post was about)

void Game::Tick( float a_DT )
{
// render a single frame here
m_Screen->Clear( 0 );
for( int i=0; i < 13; i++ )
for( int u=0; u < 10; u++ )
{
theSprite.SetFrame( i );
theSprite.Draw( i*50, u*50, m_Screen );
}
}


I added too many { and } hehe, thought I had to place { between the two for loops.

thanks for helping !

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users