6 Replies - 1410 Views - Last Post: 10 December 2012 - 06:23 PM Rate Topic: -----

#1 schroeder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-December 12

GOTO Command Alternantives?

Posted 10 December 2012 - 12:27 PM

Hey there,

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 :smile2:

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.

Any insight here would be greatly appreciated!

Is This A Good Question/Topic? 1
  • +

Replies To: GOTO Command Alternantives?

#2 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 12:29 PM

Functions, loops, switches, if and elses.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

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.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,700
  • Joined: 19-March 11

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 12:42 PM

View Postshurd, 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.

So you might have a question:
"Quien es mas macho?\n
1) jon.kiparsky\n
2) baavgai\n
3) Ricardo Montalban"

and an answer: 3

Since the computer never has to know about the content of the answers in this version, this works.

Later, you might want to dynamically generate different orderings of the answer sets, or do other clever stuff, and then you'd have to do things a little differently, but that's for later on.

This post has been edited by jon.kiparsky: 10 December 2012 - 01:01 PM

Was This Post Helpful? 3
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 12:54 PM

View Postschroeder, on 10 December 2012 - 02:27 PM, said:

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.

This is possibly the kind of goto thing you're thinking of:
top:
// get user choice
if (choice==1) { goto foo; }
if (choice==2) { goto bar; }
if (choice==3) { goto baz; }

oops:
	// tell the user they made a bad choice

foo:
	// ...
	goto done
	
bar:
	// ...

baz:
	// ...

done:
	// ask play again
	// if true
	goto top
	// ...



Notice how "bar" doesn't goto done? See how easy it is for code to bleed a little? Not a big deal in a small project, but as complexity rises, so does the evil goto can do.

Now, the above example in a more procedural way:
bool done = false;
while(!done) {
	// get user choice
	if (choice==1) { foo();
	} else if (choice==2) { bar();
	} else if (choice==3) { baz(); 
	} else { // tell the user they made a bad choice
	}
	// ask play again
	// set done
}

void foo() { /* ... */ }
void bar() { /* ... */ }
void baz() { /* ... */ }



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.

Hope this helps.
Was This Post Helpful? 3
  • +
  • -

#6 schroeder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 10-December 12

Re: GOTO Command Alternantives?

Posted 10 December 2012 - 01:36 PM

Thanks for all of the replies everyone!
I've uploaded my source code to: this page on the forum, I would greatly appreciate it if you could take a look - it may help you better answer my question.

Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

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.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1