Results 1 to 3 of 3
  1. #1

    Need a little Help with Borland C++

    Hope this is the right place to post this. Its probably a simple answer but its eluded me.

    In my program i have 15 images labelled img_0, img_1, img_2... img_14.

    I have created a random number and stored it in the integer 'i' using :

    i = rand()%14;

    Now i want to be able to select my images at randomly using the random value in 'i', by doing something along the lines of:

    img_i->Visible = true;

    However i keep getting an error saying the value assigned to 'i' isn't being used, which means i am not using it properly. I have tried various brackets around it but no avail.

    Here is the complete code for that section.

    do
    {
    int i;
    i = rand()%14;
    if (img_i->Visible == true)
    {
    lbl_count->Caption = StrToInt(lbl_count->Caption) -1;
    img_i->Height = 0;
    img_i->Width = 0;
    img_i->Visible = false;
    lbl_pick->Caption = StrToInt(lbl_pick->Caption) -1;
    }
    }
    while (StrToInt(lbl_pick->Caption) > 0);

    Thanks for any help.

  2. #2
    Join Date
    Jul 2002
    Location
    Victoria, Australia
    Posts
    36,941
    Moved to Programming Discussion.

  3. #3
    Join Date
    Aug 2000
    Location
    Redmond, WA
    Posts
    310
    I believe that your issue is due to the use of "img_i". In C/C++ this statement says that you are accessing variable img_i (not img_9 or img_0 like some scripting/interpreted languages) and checking the status of it's Visible property. Your code does not actually use i at all after assigning the random number to it. The best option that I can come up with at this moment would be to load your images into an array and use i to index into it. It would look something like this:
    PHP Code:
    Images*[] img = new Images*[15];
    /* 
    Do stuff to initialize the Image objects and store them in the array
    */

    do
    {
    int i;
    rand()%14;
    if (
    img[i]->Visible == true)
    {
    lbl_count->Caption StrToInt(lbl_count->Caption) -1;
    img[i]->Height 0;
    img[i]->Width 0;
    img[i]->Visible false;
    lbl_pick->Caption StrToInt(lbl_pick->Caption) -1;
    }
    }
    while (
    StrToInt(lbl_pick->Caption) > 0); 
    Something like this is just about the only way to do what you are attempting in C/C++. Just a note that my C/C++ is somewhat rusty so don't take it as gospel, but it should give you a nudge in the right direction. I hope this helps.
    Chris Spangler
    chris@thespanglers.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •