• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

GOTO statement

 
Ranch Hand
Posts: 543
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm bored and I'm making a quick and simple x and o's console game and I game to a dilemma in c and c++ I could get around this dilemma with the GOTO statement I know it's frowned upon using it even in them languages but that statement would make sense in that case so the problem occurs at line 62 to 66 after this block is finished I would like to loop back to line 56 so the user gets a chance to enter a choice in again if he/she entered an invalid choice in,I'm not sure how I could get around this without a GOTO statement any suggestions ?

thanks here is the code

 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use a loop, probably a do/while loop.  You'd have to refactor your code somewhat, but the basic idea would be this:
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant believe how much I have changed. I used to think a GOTO was not a problem. I don't believe that any more. I posted a rant saying we should dump OO and just go top-down procedural when I was a new member here
 
Rancher
Posts: 4614
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a pointer, the code in playerOneTurn and playerTwoTurn is almost identical, the only difference being whether it uses an 'X' or an 'O', and whether it talks about player 'One' or 'Two'.
You could turn that into a single method with one or two parameters.
 
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you wouldn't want players as parameters. There shou‍ld be player classes and objects. Each player would take turns, rather than that being done by static methods in another class.
 
Dave Tolls
Rancher
Posts: 4614
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that would be the next step.

Rather than passing in the symbol and "player name" (ie 'One' or 'Two') the method would be on an object of a Player class that would already know who they were, and what symbol they were playing.
    Bookmark Topic Watch Topic
  • New Topic