C++ Tutorial problem with part 4: loops

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 10:42 c++ sprites

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 !

8 Replies

Please log in or register to post a reply.

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 10:44

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

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 10:58

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

6aa952514ff4e5439df1e9e6d337b864
0
roel 101 Mar 17, 2012 at 11:13

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...)
 {
 }
}
D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 11:53

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 !

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 11:58

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

D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 12:06

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 );
D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 12:10

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 );
}
D7f3efa27b5340baf2ba145b259a53d1
0
RichardvBr 101 Mar 17, 2012 at 13:26

Nevermind I got it :D

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 !