Thread: goto loop;

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    goto loop;

    When i try to compile my code it gives me the following error:

    "C:\Program Files\Microsoft Visual Studio\MyProjects\Calculator20\Calc20.cpp(99) : error C2065: 'compute' : undeclared identifier"

    I basically want to know how to "declare identifiers" as my compiler puts it.

    An example of the code i have is:

    Code:
    ...
    int main()
    {
      compute; //-- I need to identify this --\\
      cout >> "Hello" >> endl;
      goto compute;
    
      return 0;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Use
    compute:


    Also, since you appear to be jsut starting - don't use goto... It can't lead anywhere good.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    int main()
    {
    compute: //-- I need to identify this --\\
      
    cout << "Hello" << endl;
    goto compute;
    
    return 0;
    }
    You >> should be << and compute should be followed by : not ;

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    oHh thanks
    what is wrong with goto?
    can u use do while with different functions?
    ie:

    Code:
    #include <iostream.h>
    
    int main ()
    {
     loop: //-- thanks for the help on that :)
     cout >> "Hello World" >> endl;
    }
    
    void loop (void)
    {
     char x
     cout >> "Press 1 if you want to re run" >> endl;
     cin << x
     
     switch (x)
     { 
      case 1:
      goto loop;
      break;
      default:
      cout >> "exiting";
    
      return 0;
    }
    or wutever, something like that

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Yea, sorry, i knew about the cout thing, just this message board doesnt have a compiler for my errors :-P

    orr a spler chek

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>what is wrong with goto?

    There are loads of threads on the virtues of goto......this is the most recent

    >>can u use do while with different functions?

    As far as I know it only has function scope so cannot jump functions

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    First off, that won't run. (besides te cout thing, you'd need to say loop() to run the function)

    Secondly, if you did happen to put a label above loop just so you could run it again, it could be better accomplished other ways. Here's one:

    Code:
    void loop (void)
    {
     char x;
     cout << "Press 1 if you want to re run" >> endl;
     cin >> x;
     
     switch (x)
     { 
      case 1:
      loop();   //It's called recursion
      break;
      default:
      cout << "exiting";
     }
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Oh yeaa, i head about that before, i just forgot.
    i think i will try to stay away from the goto command.

    cout << "Press 1 if you want to re run" >> endl;
    i dont think that will run because of the >> u have

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    If you must use a non-local jump you can do it like this -

    Code:
    #include <iostream.h>
    #include <setjmp.h>
    
    jmp_buf jb;
    void loop();
    
    int main ()
    {
    
    	setjmp(jb);
    	cout << "Hello World" << endl;
    	loop();
    	return 0;
    }
    
    void loop (void)
    {
    	char x;
    	cout << "Press 1 if you want to re run" << endl;
    	cin >> x;
     
    	 switch (x)
    	{ 
    	case '1':
    		longjmp(jb,0);
    		 break;
    	default:
    		cout << "exiting";
    	}
      
    }
    People will moan at you for using goto, but they may shout at you for using setjmp() and longjmp(), so look for an alternative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM