goto

Is it okay to use “go to” from a catch statement

做~自己de王妃 提交于 2019-12-01 17:35:05
Everything I have ever been told is that go to's are evil and stay away from them, but I think they may help me here (?). I would like to provide the user an option to restart the application when an exception is caught and am having a bit of trouble wrapping my head around what to do... My application will be monitored by another process, but there are some exceptions where I want to the user to be able to decide what to do without returning control to the calling process. Is something like this "acceptable"? Any other suggestions? Thanks so much! int main(){ initialize: try{ //do things }

Will using goto cause memory leaks?

跟風遠走 提交于 2019-12-01 16:27:19
I have a program in which i need to break out of a large bunch of nested for loops. So far, the way most people have been telling me to do it is to use an ugly goto in my code. Now, if i create a bunch of local stack (i think that's what they are called, if not, i mean just regular variables without using the new command) variables inside my loops and my program hits that one if statement that triggers the goto, will i encounter a memory leak due to my program exiting many loops improperly and not cleaning up the local variables? No, you will not cause a memory leak. Using a goto is not

C++ user input restriction with proper retry without “goto”

拥有回忆 提交于 2019-12-01 11:44:12
I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] && lname[i] == ' ' || lname[0] == ' ') { cin.clear(); cout << "Please input your lastname properly!\n";

C++ user input restriction with proper retry without “goto”

你说的曾经没有我的故事 提交于 2019-12-01 07:36:34
问题 I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] &&

Goto statements in Java [closed]

断了今生、忘了曾经 提交于 2019-12-01 02:00:45
I executed the below code in Eclipse , but the GOTO statements in it are not effective. How do I use it? How do I rewrite the above code using the Break and Continue statements without using the goto statement? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * */ /** * @author Home * */ public class student { /** * @param args */ String average(float sub1,float sub2,float sub3) { float average = (sub1+sub2+sub3)/3; if( average > 50) return "PASS"; else return "FAIL"; } String addName(String name) { return name; } public static void main(String[]

my goto redirect is not working but works with echo

左心房为你撑大大i 提交于 2019-11-30 22:48:22
@echo off :start1 set /p input=action : for /f "tokens=1-2 delims= " %%a in ("%input%") do ( goto :%%~a_%%~b >nul 2>&1 || goto start1 ) if I put "| | echo your input is not recognized" it works, but the "goto start1" crashes the script :explore_room @echo room explored goto start1 pause :examine_door @echo door examined pause :examine_wall @echo wall examined pause @echo off :start1 set /p input=action : call :%input: =_% 2>nul if errorlevel 1 echo your input is not recognized goto start1 :explore_room @echo room explored pause exit /B 0 :examine_door echo door examined pause exit /B 0

Why is it OK to jump into the scope of an object of scalar type w/o an initializer?

拜拜、爱过 提交于 2019-11-30 20:39:58
When I'm reading the C++ standard, it seems that the following code is perfectly fine according to the standard. int main() { goto lol; { int x; lol: cout << x << endl; } } // OK [n3290: 6.7/3]: It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type , class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types

my goto redirect is not working but works with echo

落爺英雄遲暮 提交于 2019-11-30 17:23:09
问题 @echo off :start1 set /p input=action : for /f "tokens=1-2 delims= " %%a in ("%input%") do ( goto :%%~a_%%~b >nul 2>&1 || goto start1 ) if I put "| | echo your input is not recognized" it works, but the "goto start1" crashes the script :explore_room @echo room explored goto start1 pause :examine_door @echo door examined pause :examine_wall @echo wall examined pause 回答1: @echo off :start1 set /p input=action : call :%input: =_% 2>nul if errorlevel 1 echo your input is not recognized goto

Goto out of a block: do destructors get called?

旧巷老猫 提交于 2019-11-30 13:01:39
问题 Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ? 回答1: The C++ Standard says: On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration. So the answer is "yes". 回答2: Yes, they will be

Is using goto a legitimate way to break out of two loops?

风格不统一 提交于 2019-11-30 11:08:37
I am solving problem 9 on the Project Euler . In my solution I use a "goto" statement to break out of two for loops. The Problem is the following: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. My solution is in c++: int a,b,c; const int sum = 1000; int result = -1; for (a = 1; a<sum; a++){ for (b = 1; b < sum; b++){ c = sum-a-b; if (a*a+b*b == c*c){ result = a*b*c; goto found; } } } found: std::cout << "a:" << a <