Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. General C++ Question (goto and switch statements)
Forum Updated to NodeBB v4.3 + New Features

General C++ Question (goto and switch statements)

Scheduled Pinned Locked Moved C++ Gurus
2 Posts 2 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thugzook
    wrote on last edited by Thugzook
    #1

    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
    K 1 Reply Last reply
    0
    • T Thugzook

      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
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved