Forums Register Login

catch statements

+Pie Number of slices to send: Send
Ive been having trouble implementing a catch statement into this method. I want to catch all values but y and n.

Any suggestions?



Thanks
+Pie Number of slices to send: Send
I'm a little confused about your question. Are you talking about creating a catch block to handle an exception? Or are you talking about checking values that the user enters with if statements like you have?
+Pie Number of slices to send: Send
catch block
+Pie Number of slices to send: Send
Errr... a catch block is half of a try/catch block, which as Keith already pointed out, is an error/exception catching block. For instance:


Are you maybe trying to do something like this?

That seems maybe backwards from what it appears that you are trying to do. Could you maybe help us out and explain what you are trying to do?

EDIT: Or are you saying that you want to see if it is a yes or no answer first, and if it isn't, then skip the code altogether?
[ December 13, 2005: Message edited by: C Clites ]
+Pie Number of slices to send: Send
ste, does this mean you're considering throwing an exception if the user enters something other than 'n' or 'y'?
[ December 13, 2005: Message edited by: marc weber ]
+Pie Number of slices to send: Send
Yea, if the user enter y, or n, then the program will do something, but anything else throws an exception. I just cant seem to get the catch to work.
+Pie Number of slices to send: Send
I'm not convinced that you want to catch anything, and I'm not sure how to explain to you that I don't think that is what you want to do. A try/catch statement is for catching program errors..

Strictly defined

Definition: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.



What I think you really want to do is something like as follows:

Some ways of handling bad input are ignoring the input (bad idea), or somehow letting the user know that their input was bad (better). I would consider that a normal event, and not one that disrupts the flow of a program.

Unless you are really interested in generating custom exceptions..
+Pie Number of slices to send: Send
Thanks, i did do that initially, but the problem is when i put something bad in, it generates the game numbers again. Ill figure it out. Try this and see what i mean. The uses is only allowed 1 roll each turn.

+Pie Number of slices to send: Send
I understand what you are trying to do, and after looking at your code a bit, I'm pretty sure you know what you need to do, although I think some restructuring is called for. I suggest maybe sitting down with your pencil and paper and thinking it through a bit... you have so many loops and whiles that it is hard to keep track of everything.

If I am following your descriptions, basically you have three rolls to get the highest score. On each of the three rolls, you have the option of saving one of the scores. After three rolls, the scores are added, and the score printed.

One place I would look at starting is

Keep the totals and amount of rolls outside of the loop.. that way they don't get reset each time.

but the problem is when i put something bad in, it generates the game numbers again. Ill figure it out. Try this and see what i mean.



What I would do in this case is that if the user puts in something bad (something other than y or n), then make it default to no (do not save the highest roll). That way, you only need to check if the user chose yes.. so you would have something like:

Or if you want, you can check if it is a y or n, and if it isn't, prompt the user for a valid input.

Another thing that may be nit-picky... you don't need an array to hold the highest values. Since the values are going to be totaled, why not just keep a single int to keep track of the running score? Then there is no need for a seperate loop to total the scores later. You only need to print out the total.

Also, you don't need an array to hold the rolls either, you just need a single int to hold the highest roll for that round. It is initially a little more tricky to code, but your code will be more simple, and easier to read.

I think the one thing that will help you the most is to use a pencil and paper and draw out your program. It should give you a much clearer idea of what you want to do...
+Pie Number of slices to send: Send
 

Originally posted by Chad Clites:
... What I would do in this case is that if the user puts in something bad (something other than y or n), then make it default to no...


Good advice.
+Pie Number of slices to send: Send
Thanks alot chad, ill ake your advice
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1366 times.
Similar Threads
need help with C++ problem
dice game
Help.
So far...
DiceGame Code - which is better?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:59:10.