General C++ Question (goto and switch statements)



  • Hello everyone.
    I've been searching online, and regarding the goto statement, goto points to identifiers.
    However, is there a way for it to point to a certain point in a switch() statement ?

    (please disregard the existing goto statement. I understand that is completely wrong.)
    I'm looking to use a random number to then call a certain case statement (from 0 - 11)
    Is there a way to do that with numbers? or no?
    Or, is there a better way to do this?

    /* fill screen with chars for game operations
     * Pre   : called on by void fightEnemy(...)
     * Param : char chars[]
     * Return: none
     * Post  : letters outputted to the game
    */
    void MainGame2::fillScreen(char chars[], int TOT_CHARS)
    {
        ///variables
        int randNum; //will house a random number 0 - 11 to simulate which label the char will reside in
        bool charLocs[12] = {false}; //will house bool status of whether or not there is a value present in the location.
    
        ///set seed
        srand(time(NULL));
    
        ///for loop to randomly input chars into labels
        for (int i = 0; i < TOT_CHARS; i++)
        {
            ///value from 0 - 11 decided
            randNum = rand () % 12;
    
            ///gather the value of chooser
            switch (randNum)
            {
                ///location 1
                case 0:
                {
                    ///check to see if there is a value present at the location
                    if (charLocs[0] == false)
                    {
                        ///make the location true
                        charLocs[0] == true;
                        ///continue with operations
                        ui->charLoc1->setText(static_cast<QString>(chars[i]));
                        ///make visible
                        ui->charLoc1->setVisible(true);
                    }//end if
                    ///value is present already
                    else if (charLocs[0] == true)
                    {
                        ///loop through until game finds a location
                        while (charLocs[randNum] == true)
                        {
                            ///value from 0 - 11 re-decided
                            randNum = rand () % 12;
    
                            ///check location , if true
                            if (charLocs[randNum] == false)
                            {
                                ///call the appropriate case
                                goto randNum;
                            }//end if
                        }//end while
                    }//end if 
                    ///break out of function
                    break;
                }//end case 0
    
    ...etc

  • Moderators

    @Thugzook
    Hi I have moved your topic to C++ Gurus. Even though it is not really a guru question. However, it is definitely no Qt related question.

    IMHO you do not follow rules with your code. A "goto" is still present in C++, but generally considered as a nogo by most programmers. You cannot jump to a specific case with a goto statement.
    switch is the point of decision and it basically does a jump to the specific case. You need to reformulate and get the while loop around your switch statement. As you try to do it it will not work.

    BTW some programmers are not happy about switch either. IIRC even Bjarne Stroustrop in his famous book recommended to use switch statement sparsely.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.