May 23, 2019, 07:08:48 am

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


For/Next and Do/Until Loops

Started by whitenite1, October 20, 2010, 12:54:45 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

whitenite1

October 20, 2010, 12:54:45 pm Last Edit: October 20, 2010, 02:08:07 pm by whitenite1
Good afternoon...

 I'm attempting to learn about these loops in Aurora, and am having a difficult time. I'm am wanting to scramble the colors in the temp[] array into the colors[] array. I want colors[1] maybe to equal temp[5], colors[2] to equal temp[1], etc. I got the first 2 or 3 in the colors array to work, but the rest of the array prints strange symbols.

But, hopefully, someone can show me what I've done wrong, and get this piece of code working...

Thanks all...

Code Select
global sub main( )
{
int num,num2,x; //An integer variable
x = 1;
string temp[9];  //an array
string colors[9];  //an array

temp[1] = "Red", "Blue", "Green", "Yellow",
         "Purple", "Burgandy", "Orange", "Violet";

do
   {
       do
       {
           num = (rnd(1.0, 8.0));
           if ( colors[num] == "" )
            {
                colors[num] = temp[x];
                print ("\nColors[",x,"]=",num,"\n",colors[x],);
            }
         x++;
        } until colors[num]!="";
       
     
   } until x==9;
     
locate (5,10);
   print("Here is a list of my favorite colors : \n\n",);
   num = rnd(1.0, 8.0);
   num2 = rnd(1.0, 8.0);
   
   for(x = 1; x< 9; x++)
   {
      print ("\n(",x,") ",colors[ x ]," ",);
      if (num == x)
      {
          print ("<= This one is my all time FAVORITE!!",);
      }
      if (num2 == x && (num <> num2))
      {
          print ("<= This one is my LEAST favorite.",);
      }
   }
   print("\n\nPress the 'F6' key to close");
   While GetKey(true) != "\x75";
}


whitenite1

LarryMc

October 20, 2010, 02:14:38 pm #1 Last Edit: October 20, 2010, 02:21:18 pm by Larry McCaughn
It wasn't printing anything because it was in an endless loop inside the while statement.

The following works:
1st I had to make sure all the color elements were "" to begin with(line 8 ), less you get "strange symbols"
2nd I had to restructure your logic.

Code Select
global sub main( )
{
int num,num2,x; //An integer variable
num = 1;
x = 1;
string temp[9];  //an array
string colors[9];  //an array
colors[1]="","","","","","","","";
temp[1] = "red", "blue", "green", "yellow",
        "purple", "burgandy", "orange", "violet";

do
  {
      do
      {
          num = (rnd(1,8));
  } until colors[num]=="";
      colors[num] = temp[x];
      x++;
  } until x==9;
   
locate (5,10);
  print("Here is a list of my favorite colors : \n\n",);
  num = rnd(1.0, 8.0);
  num2 = rnd(1.0, 8.0);
 
  for(x = 1; x< 9; x++)
  {
     print ("\n(",x,") ",colors[ x ]," ",);
     if (num == x)
     {
         print ("<= This one is my all time FAVORITE!!",);
     }
     if (num2 == x && (num <> num2))
     {
         print ("<= This one is my LEAST favorite.",);
     }
  }
  print("\n\nPress the 'F6' key to close");
  While GetKey(true) != "\x75";
}

This line of code will result in there being no LEAST favorite at times:
Code Select
if (num2 == x && (num <> num2))

Took me a little while because I don't like the {} and ; of Aurora - just a personal preference thing.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

why do you find it necessary to keep printing the same string over and over while you are waiting for a certain key to be pressed?

Try this:
Code Select
global sub main( )
{
   string a$ = "Please press the 'F6' key to close - ";
   a$=mid$(a$,2)+left$(a$,1);
   locate (2,15);
   print (a$);
   while(GetKey(true) <> "\x75")
    {
wait;
    }
}


LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

I missed that.
If it was IWBasic I would say use the timer function and put the text stuff in the ontimer event.

But I don't see the equivalent in Aurora.

I would still use my while loop.
inside it I would use call to get the systime  and test if the delta in current time and last time is whatever you want the speed to be and if the delta is big enough uopdate your text and set last time to current time.
you can use the TIME$ function if one second is fast enough.
otherwise I guess it would have to entail the use of some API

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

sapero

Sleep function is aboard! Use #include "windows.inc" for the Sleep or _Sleep declaration, or declare it:
Code Select
declare import, Sleep(int ms)