#### Archived

This topic is now archived and is closed to further replies.

# I have a question about loops...

This topic is 6799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was attempting to make a program that took the caption of a window, and scrolled it back and forth, I got it to work using one technique (2 for loops, one for each direction), but my original idea wasn't working....I wanted to have a single while and for loop structure like so : while(cnt<=60) { for(int x = 0;x < cnt%12;x++) buf += " "; // don't worry about this, it works =) buf += "Hello Bob"; SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)buf.c_str()); cnt++; } but this only goes one way (right) and jumps back to 0 and i tried it a different way and got it to work coming back, but it didn't work going forward, it just stayed put half the time, I don't have to have this way to work, but it would be nice to see how it would work since i couldn't figure it out. To sum it up, the basic jist is that I want a formula to make a for loop oscilate from 0 to N in both directions (probably using mod) with only one statement. Edited by - BraveZeus on 3/30/00 1:02:54 AM

##### Share on other sites
OK... your code and description is a little cryptic for me, but if I understand what you''re trying to do, correct me if I''m wrong, is you want to move the text, "Hello Bob" from the left side of the window caption to the right, then back again. If this is so, then the solution could be something like this or there''s something probably a lot simpler, but oh well... this is a fairly basic problem, and I''m probably not understanding what you meant... oh well:

int inc = 1, int pos = 0, caption_width = 60;
while( true ) //REPLACE "true" WITH CONDITION
{
buffer = "";
pos += inc;

if( pos >= caption_width // pos <= 0 )
inc = -inc;

for( int i = 0; i < pos; i++ )
buffer += " ";

buffer += "Hello, Bob";
SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)buffer.c_str());
}

-Derek

##### Share on other sites
That makes sense... But I think I know a more efficient way... correct me if I''m wrong!

I can''t remember the command for writing text to a window... something like PaintText(x,y,"Hello World", sizeof("Hello world")) I will use that format here just so I don''t have to go look up the actual function. You can do that

int xpos = 0,ypos = 0,inc = 1;
char Buffer[] = "Hello World";

while (true) // replace with condition
{
PaintText(xpos, ypos, Buffer, sizeof(Buffer));
xpos += inc;
if (xpos + sizeof(Buffer) * 6 >= SCREEN_WIDTH // xpos <= 0)
inc = -inc;
}

--------------------

You are not a real programmer until you end all your sentences with semicolons;

##### Share on other sites
Well, if you really want a one line fix...

change:

for(int x = 0;x < cnt%12;x++)

to:

for(int x = 0;x < (12 - abs(12 - (cnt % 24)); x++)

##### Share on other sites
you could also respond to the WM_NCPAINT message. that would give you more control.

##### Share on other sites
Why not use the sprintf() function with the variable width padding parameter?

Here''s a little program I put together..

#define MAX_PADDING   20char buffer[1024];void UpdateBuffer(char *message){  // initialize static variables; these are not reset each function entry  static int padding = 0;   static int incrementer = 1;  // increment or decrement the padding  padding += incrementer;  // build the buffer  sprintf(buffer,"% *s%s", padding, "", message);  // switch the sign of the incrementer if  // the padding has reached a maximum value  if (padding == MAX_PADDING)    incrementer = -1;  if (padding == 0)    incrementer = 1;  return;}int main(int argc, char* argv[]){  int i;  for (i = 0; i < 100; i++)  {    UpdateBuffer("hello");    printf("%s\n", buffer);  }	return 0;}

See how there is an integer in sprintf (padding) which controls how much padding an empty string "" gets? I increment or decrement the padding so that more or less spaces get put infront of the "". I then put the actual string I want printed on the end.

##### Share on other sites
Or you can get Linux and X-windows, the titles automatically scroll.

I think it''s a good idea, especially with programs that use looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong titles.

And everybody thought Gates had all the inovation . . .

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633295
• Total Posts
3011245
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!