English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm in computer programming right now and I'm stuck. I made a program and it works fine, but if somebody enters a number that's not an option, my teacher wants it to go back to the start of the program, and I don't know how to do that. Any help?

2007-03-13 09:33:20 · 5 answers · asked by Kaitlyn 2 in Computers & Internet Programming & Design

5 answers

It really depends on the language you are using. What is it?

2007-03-13 09:36:25 · answer #1 · answered by Elizabeth Howard 6 · 0 0

You can create a label at the start of your code section

BEGIN:
your code goes here and correct numbers
will either exit the subroutine or GOTO SUCCESS
GOTO BEGIN
SUCCESS:


This technique is mainly used in a procedural language and is considered bad form in an Event driven language like VB. However, VB does support goto's and labels. You must be careful to always provide a means to exit the loop in your menu choices. Otherwise you can easily generate an endless loop or force the user to choose an undesired operation.

Another reason to avoid goto's is that its easy to write spaghetti code.

2007-03-13 19:01:37 · answer #2 · answered by MarkG 7 · 0 0

create a if at the beginning of the routine
check the data variable. If your good numbers are between 200 and 300 then: Variable holding your number value is called counter.
The If statement:

If counter < 200 or counter > 200 go to
START OVER
else do the rest of your routine with the valid number.

2007-03-13 17:09:05 · answer #3 · answered by Anonymous · 0 0

If it is in VB6 just "Exit Sub". Although the basic example above would do the same - without the need for line numbers.

2007-03-13 17:36:52 · answer #4 · answered by Mike James 1 · 0 0

A. I'm assuming you are using an imperative language like C, C++, Java, etc.

B.

easiest method. (shown in Java)
boolean valid = false;
while (valid)
{
System.out.println("Please enter a choice... blah balh bah");

if (answer <= max && answer >= min) valid = true;
}

2007-03-13 16:40:45 · answer #5 · answered by tster 1 · 0 0

fedest.com, questions and answers