Goto statement

This is a discussion on Goto statement within the C Programming forums, part of the General Programming Boards category; when i have goto master, should the _menu() not be executed again. what is happening is that the function is ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Question Goto statement

    when i have goto master, should the _menu() not be executed again. what is happening is that the function is not being executed and the switch is executing the next case in the list. help me out please.

    Code:
    /*Main----------------------------------*/
    /*--------------------------------------*/
    int main()
    {
          top:
    
          /*validate master user*/	
          if(_validate_master(_menu())!=1)
          {
    	  goto top;
          }else{
    
          master:
    	
    	 switch (_master_menu()){
    	    case 1:   if(_newaccount()!=0){goto master;}	/*if insert unsuccessful goto master main*/
    	    case 2:   if(_cust_cntrl()!=0){goto master;}
                // case 3:
    	    default:  	goto master;
    
             }
          }
    
    
          
    
    return 0;         
    }
    /*--------------------------------------*/
    Last edited by _JjC::; 03-02-2003 at 10:19 AM.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Ehm, my English is not very good, but I'll try to understand your question. If you reach the line with "goto master", then the program goes to the line labeled "master", but it doesn't. Perhaps that is because _new_account() returns 0? In that case the next case will be executed. To prevent from executing, you should use the keyword break.

    Code:
    switch (condition)
    {
      case X:
        /* Action */
        break;
      case Y:
        /* Action */
        break;
      default:
        /* Action */
        break;
    }
    BTW, why do you use goto? Also use code-tags, they make code more readable.

    Code:
    int main ()
    {
      int result = 0;
    
      while (result != 1)
        result = _validate_master ();
    
      /* If the while-loop above finishes, then result == 1 */
    
      while (result != 0)
      {
        switch (_master_menu ())
        {
          case 1:
            result = _newaccount ();
            break;
          case 2:
            result = _cust_cntrl ();
            break;
          default:
            break;
        }
      }
    
      return 0;
    }
    Last edited by Shiro; 03-02-2003 at 09:22 AM.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hey don't forget to use break in your switch statements, most of the times you will need to break them, else, not just one case will run, but, all other cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GOTO statement in c++
    By kibestar in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2009, 07:10 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  4. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 09:37 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM

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