Results 1 to 6 of 6

Thread: equivalent of vb's GoTo

  1. #1

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197

    equivalent of vb's GoTo

    How can I do a Goto in c++?
    retired member. Thanks for everything

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you jump to a label lbl:

    goto lbl;

    you declare a label lbl:

    lbl:

    Why do you need goto? If you don't know how to code algorims in general, avoid goto at all cost, if you want to go low level use of goto is ok. There's some selective cases where a goto would be a lot cleaner, but in general it's recommended for other than advanced programmers.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Im going a basic comsole thing; since i dont know error stuff yet, it check if the entered data is right, if not, goto some label.
    Im still learning
    retired member. Thanks for everything

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by markman
    Im going a basic comsole thing; since i dont know error stuff yet, it check if the entered data is right, if not, goto some label.
    Im still learning
    Aaaaaaccckkkkk

    Read the input.

    If it's not valid, throw an exception
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    Originally posted by parksie
    exception
    Is that a c++ term?
    retired member. Thanks for everything

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep. The idea is that rather than errors being either handled too early or having to be manually propagated, you just throw an exception and the system unwinds the function call stack searching for a compatible catch block to handle the exception. If nothing catches it, then the system closes the program down.

    So, library functions can throw exceptions, and it's a lot simpler to handle for you. For example:
    Code:
    int* mynew(int count) {
        int *ptr = new int[count];
    
        for(int i = 0; i < count; ++i)
            ptr[i] = -1;
    
        return ptr;
    }
    
    void somecode() {
        int *ptr = mynew(50);
    
        // Do some processing
    }
    
    int main() {
        try {
            somecode();
        } catch(exception &ex) {
            // Standard library exceptions all derive from exception
            cout << somecode.what() << endl;
        } catch(...) {
            // ALL exceptions will be matched by this
            cout << "Other exception" << endl;
        }
    
        return 0;
    }
    The point here, is that if new fails to allocate the memory, it throws an exception of type bad_alloc (standard class, derives from exception). This doesn't have to be caught by somecode, but main catches it. It means that exceptions are handled at a higher level, by handler code that understands more of the application, and therefore what the best solution to resolve the problem is.

    NB: Anything can be thrown, for example an int.

    Read Thinking in C++ for more information
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width