arrays within switch statements

This is a discussion on arrays within switch statements within the C++ Programming forums, part of the General Programming Boards category; Hello I posted a question on this forum about a similar issue but i still have not resolved it. Basically ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    Question arrays within switch statements

    Hello

    I posted a question on this forum about a similar issue but i still have not resolved it. Basically i'm writing a small app- the visual display consists of 5 circles and a Next button. The circles are grey to begin with, but turn red one at a time as you hit the next button.
    I'm assuming that an array and a switch statement is the best thing to use for this, so have written the following:

    void __fastcall TForm1::NextButtonClick(TObject *Sender)
    {
    int StateArray[6]= {1,2,3,4,5,6};

    switch(StateArray)
    {
    case 1 : Shape5->Brush->Color = clRed; break;
    case 2 : Shape4->Brush->Color = clRed; break;
    case 3 : Shape3->Brush->Color = clRed; break;
    case 4 : Shape2->Brush->Color = clRed; break;
    case 5 : Shape1->Brush->Color = clRed; break;

    }
    }

    The error i'm getting is that the switch selection expression must be of integral type. First, I want to know if i am on the right track above (more or less). Secondly, i understand now that the case statements need to be converted to int, but i cannot find code on how to do this anywhere. Any help MUCH appreciated.

    Divinyl

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    All your answers can be found here

  3. #3
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Well, you can't use arrays in switch statements like that... The name of an array is a pointer to the first element of the array, and you can't have a pointer in a switch statement. I know you can use integers and characters and probably other primitive data types in switch statements.... Now I'm not sure exactly how your StateArray relates to your five Shape objects, but it looks like you just want a list of numbers to use in the switch statement. Maybe you could try something like this:

    Code:
    void __fastcall TForm1::NextButtonClick(TObject *Sender)
    {
    
      int state = 1;  // Somehow set the value to 1, 2, 3, 4, or 5
    
      switch(state)
      {
        case 1 : Shape5->Brush->Color = clRed; break;
        case 2 : Shape4->Brush->Color = clRed; break;
        case 3 : Shape3->Brush->Color = clRed; break;
        case 4 : Shape2->Brush->Color = clRed; break;
        case 5 : Shape1->Brush->Color = clRed; break;
    
      }
    }
    Or perhaps state could be an index to your StateArray, and the switch statement could be:

    switch (StateArray[state])

    Is that what you're trying to do?

    EDIT: I must type slow, looks like two others beat me to it
    My programs don't have bugs, they just develop random features.

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>EDIT: I must type slow, looks like two others beat me to it

    only one answered his question. I dont read code without the tags so i jus guided him to the tag information page hoping he'd use em.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    5
    Hi- and thanks for the replies (and the tips about the code tags!). Well, i have received some help and this is what i've got:

    <code>


    void __fastcall TForm1::NextButtonClick(TObject *Sender)
    {
    static int State = 0;
    numlist++;
    {
    switch(numlist)
    {
    case 1 : Shape5->Brush->Color = clRed; break;
    case 2 : Shape4->Brush->Color = clRed; break;
    case 3 : Shape3->Brush->Color = clRed; break;
    case 4 : Shape2->Brush->Color = clRed; break;
    case 5 : Shape1->Brush->Color = clRed; break;
    case 6 : Shape5->Brush->Color = clWhite; Shape4->Brush->Color = clWhite; Shape3->Brush->Color = clWhite;Shape2->Brush->Color = clWhite;Shape1->Brush->Color = clWhite; break;
    }
    }
    }

    </code>

    This works very well- each of the lights turn on as the Next button is clicked, and all lights turn off on the 6th click. However, i need it to loop through steps 1-6 indefinitely- the way it is at the moment, after step 6 (when all lights are "off") it stops. Any suggestions?

    Thanks

    Divinyl

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The code tags still don't work
    Read the first link again.


    To restart the loop, set numlist to 0 in the case block for 6.

    And remove the State variable, you obviously don't use it.

    And you might want to split the code to several lines, makes it easier to read.

    And you have a redundant pair of braces.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    5
    thank you corned bee, and the rest. The program works well now.
    Divinyl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  3. switch statements with greater/less than operators
    By Munkey01 in forum C++ Programming
    Replies: 7
    Last Post: 02-13-2003, 11:59 AM
  4. Switch statements
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21