GOTO Command Alternantives?

I decided to start teaching myself C++ this weekend - before I get started with my post, I should apologize in advance for the n00b question - take it easy on me

I would also like to preface my question with a description of the program I am writing (as I am not currently home, and can't supply my source code for you to view):

For my first project, I decided to go a bit beyond the cliche "Hello, World" program.
I made a multiple choice quiz - to help my girlfriend study for a state-regulated exam she has coming up.

Right now, it consists of 25 questions - all with multiple choice a, b, c or d answers.
If you get the answer correct, you are told so, and get to move on to the next question.
If you get the answer incorrect, you are told the correct answer, and get to move on to the next question.
At the end of the quiz a score is given.

I would like to expand on this program - offering multiple "sections" of study.

Ideally, you would be greeted by a "table of contents", from which you could select which section you would like to study from. Once you make a selection, it would jump you to that part of the quiz.

I did some research online, and saw that some people use the "GOTO" command to jump to specific sections of their code.... With that said, I saw a good amount of articles that mentioned using the "GOTO" command as being a bad practice for good c++ programming.

So - what alternatives do I have to the "GOTO" command?
I really like how easy it is to just place a marker/label in your code, and jump straight to it - I don't really understand why this would be a bad thing to do.

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 12:30 PM

The reason people say it's bad to use GOTO, is because if you don't understand what it does exactly, you can get into trouble real quick. I would suggest writing each "section" and the "table of contents" as their own separate functions. Then in main(), you just have a loop that asks what the user wants to do, and then use a switch-statement on their selection. Depending on what the user picks, you just call the appropriate function.

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 12:42 PM

shurd, on 10 December 2012 - 02:29 PM, said:

Functions, loops, switches, if and elses.

To expand on this a little, could consider something like this:

A "quiz" is a series of questions. Each one has a question and a set of potential answers. Assuming there are always N potential answers, these can be represented as some multi-line text and one integer in the range 1..N representing the correct answer.

To keep things simple, you could do this with two arrays, one containing the questions and the other containing the answers. So now you have an array of strings and an array of ints.

Now, the part you're trying to get to:

Suppose you have 25 questions. You'd use a for loop to iterate over the list of questions. In the loop, you'd print the text of the current question and ask the student for an answer. You'd get the input and if it's incorrect you'd ask again, otherwise you'd continue to the next question.

roughly like this (sketch, not running code):

for (int i = 0; i <25; i ++)
{
userAnswer = -1;
while (userAnswer != answer[i])
{
display(question[i]); // send it to the console by whatever means
userAnswer = getAnswerFromUser(); // whatever you do to get the input, do that
}
}

So I've used two types of loop here, which you should study if you don't understand them. The for loop is actually a specialized sort of while loop, and can be transparently rewritten as a while loop, but you can learn them as separate types if that's easier.

EDIT: For the sake of clarity, I want to make this explicit. The "Question" here includes the set of possible responses, and the "Answer" is simply the number of the correct alternative.

Re: GOTO Command Alternantives?

I really like how easy it is to just place a marker/label in your code, and jump straight to it - I don't really understand why this would be a bad thing to do.

We have little holy wars about the evils of GOTO floating around here. Check them out, they're fun. The GOTO apologist is just as earnest as the GOTO demonizer, but they are in the extreme minority for a reason.

Your program should have functions and scope, not large blocks of unscoped mess.

Simply, there is nothing you NEED a goto for in a procedural language. A carry over from other languages and other eras. If you're doing BASIC or Assembly language, use GOTO. If you're doing C or C++, look for better options.

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 06:23 PM

When you use GOTO's in your program, you slip into a different, and less organized mind-set. When dinosaurs ruled the earth, I went to a College that had their admissions process handled by a program written in BASIC - and FULL of goto's. At that time, BASIC HAD no loop - you used goto's to create them, and no function calls - you used goto's or gosub's.

Well, everything worked fine, it was all very professionally done - but then they needed to alter the program. So they went back to the programmer who wrote it, but he was involved with a big project - and it would be 2 years before he could even get started on the program.

They went to other programmers - but they took one look at all the spaghetti with the goto's, and said "It would be much cheaper to write you a new program from scratch."

And that's what they had to do. Fortunately, the languages had improved - C and Pascal were both out by then and even BASIC was a lot better.

I've got an old chess program that was written in BASIC, with goto's - and it's just insane to try and figure out the logic, from looking at the code - and it has no comments, of course.

Anybody can write indescribably opaque code, in almost any language, but goto's just make it much more probable that the code will wind up being hard to understand.

I use goto's to quickly get out of nested loops, in time-critical code, and that's it (OK, 99.9% of it anyway.)