Articles

Solved Goto command (C++)

February 20, 2005 at 14:22:47
Specs: XP Home, 512 MB

How the goto command, and how do you set the place where you want it to go to?

See More: Goto command (C++)

Report •


#1
February 20, 2005 at 15:30:17

First, if you mean GOTO as in "go to this location on the screen, based on X and Y coordinates" then it depends on your compiler. Some come with libraries that allow you to do this, others do not. Take a look here for more info including a function to do what you want.

Now, if you mean GOTO as in "go to a specific line or label in my program based on some condition" then that is bad, bad, BAD!

Using GOTO commands in modern languages is a BIG faux pas in programming. It should be avoided, and more structured, well, structures should be used. Instead of GOTO, use a loop, conditional, or function call. You've got lots of options in C++: FOR, WHILE, DO, IF, SWITCH, TERNARY (?). Other times you'll want to use a function call in place of a GOTO jump.


Report •

#2
February 20, 2005 at 18:20:21
✔ Best Answer

int main()
{
char choice;

top: // goto label

cout << "Hello World!" << endl;
cout << endl;

cout << "Do again? (y/n)" << endl;
cout << "- ";
cin >> choice;
cout << endl;

if (choice == 'y')
goto top; // goto statement

return 0;
}

There's a religious battle amongst computer scientists regarding the use of the goto statement. Some say yes, some say no -- it is possible that the use of gotos can make code unbelievably difficult to read. The term spaghetti junction is used to describe confusing code that is written with goto statements. I won't say that I'm for or against it. I use it sparingly and I use it when I see that it is fit to do so.

- Rolos


Report •

#3
February 21, 2005 at 15:28:03

Thanks, ill keep that structuring thing in mind...i love this forum

Report •

Related Solutions

#4
February 24, 2005 at 00:42:40

While you're learning how to program, you should take the time to understand why modularization is good, and why you should keep in mind Object Oriented programming.

For your case, you should make a function for things that are to be done repeatedly (or even called once, but have a special duty).

----

For example, in Rolo's case above (haven't programmed in 2 years, so i'm just going to give you the gist of it):

//function prototypes (you list the functions
//in the beginning...so the compiler knows
//what to expect
char menu();

void main()
{
char choice='y'; //default = y

while (choice=='y')
choice=menu();
}

char menu()
{
char x='n';
cout << "Hello World!" << endl;
cout << endl;

cout << "Do again? (y/n)" << endl;
cout << "- ";
cin >> x;
cout << endl;
return(x);
}

---

The beauty of this is that you can call a function (like menu()) from anywhere in the program (you don't need to remember a line number, etc.) And, it clears up your program. Notice how small the main function is. You can easily read how the program flows from one place to another...if you don't know what menu() does, you just jump to the menu() function to see what it does. This is modularity. If you make a nice function, like something that calculates so-and-so, you might take that function in the future and incorporate it into dozens of future programs (thus not having to re-invent the wheel).

Object Oriented programming goes a step further, where you create objects that contain their own data, and you create interfaces to access those objects. Like you might make a Desk object, with the data being a linked list to what you've put in the desk. Interface would be (put_item_in_desk() and (take_item_outof_desk()...that way you can control the integrity of the data).

C++ has the STL (standard template library), which are basically pre-built objects that you can use (they're templates). You basically just add functionality to them (like maybe you want to add a color variable) ...you create objects that inherit their properties. That's another story for later, though. =)

Dr. Nick is right though, you will probably never see a GOTO statement in a program written in a language capable of doing OO.

But once you kick the basic habit and get used to C++, you'll not only enjoy it alot, but you'll be able to pick up on other languages (like Java) pretty fast.


Report •


Ask Question