|
-
Dec 10th, 2001, 09:22 PM
#1
Thread Starter
Frenzied Member
equivalent of vb's GoTo
How can I do a Goto in c++?
retired member. Thanks for everything 
-
Dec 10th, 2001, 09:29 PM
#2
transcendental analytic
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.
-
Dec 11th, 2001, 12:57 PM
#3
Thread Starter
Frenzied Member
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 
-
Dec 11th, 2001, 03:12 PM
#4
Monday Morning Lunatic
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
-
Dec 11th, 2001, 04:30 PM
#5
Thread Starter
Frenzied Member
Originally posted by parksie
exception
Is that a c++ term?
retired member. Thanks for everything 
-
Dec 11th, 2001, 04:39 PM
#6
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|