3 Replies - 270 Views - Last Post: 22 August 2012 - 04:12 PM Rate Topic: -----

#1 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Exception Handling Noob

Posted 22 August 2012 - 12:58 PM

Hey !

I got a little text-based RPG going on . It's just so I keep in touch with C++ while learning C# , however in Charles Petzold's .Net Book Zero I ran into exception handling which is fairly easy in C# .
Now , I was aware of this technique before I started to learn C# but never paid attention to it (big mistake !)

I want the player to choose one of the following options and if the input isn't adequate it should ask again and again until the end of time (yes, I know it can easily be done with if-else but I want it this way so I'll learn something new . I looked into CPlusPlus.com and CProgramming.com but I don't feel like it's helping a lot .

The code will run but once I enter an invalid input the do-while statement will loop forever and ever . Any ideas ?

Thanks in advance!

do
	{
		try
		{
			cout << " What would you like to do ? \n" <<
				"1.Start A New Game\n"<<
				"2.Load A Game\n"<<
				"3.View Credits\n"<<
				"4.Exit Game\n";

			cin >> choice ;

			if (choice < 1 || choice > 4) throw NO_OPTION;
			//else if (!isdigit(choice)) throw NO_DIGIT; <- also it seems to work better if i do the following
                        else if (isdigit(choice)) throw NO_DIGIT; //why ? cplusplus.com doc says that
                                                       //isdigit should return 0 if choice is not int and true 
                                                     //if isdigit is int 
		}

		catch (int ex)
		{
			if (ex == NO_OPTION)
			{
				cout << "Invalid option number\n";
			}

			if (ex == NO_DIGIT)
			{
				cout << "The option selected is not a digit\n";
			}

			choice = -1 ;
                        //cin.clear(); <- thought it would get the same input every time but it doesn't
                        //it only loops forever when I enter chars, otherwise it 
		}
	} while ( choice == -1) ;


This post has been edited by TwoOfDiamonds: 22 August 2012 - 12:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Exception Handling Noob

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Exception Handling Noob

Posted 22 August 2012 - 01:20 PM

What type of variable is choice? If it is a numeric type and you try to enter a character will cause your stream to enter an error state. You will need to clear the error and then empty the stream buffer before you can try to get any more input from the stream.

Also in my opinion this is not really something that I would use an exception to handle. To me this is not exceptional, but normal data validation.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,574
  • Joined: 19-February 09

Re: Exception Handling Noob

Posted 22 August 2012 - 01:52 PM

Hi, here you have

if choice is a digit then throw NO_DIGIT.

15       else if (isdigit(choice)) throw NO_DIGIT;




You probably want if choice is not a digit - if(!isdigit(choice))


Possibly you have choice as an integer. In that case if a non-digit is entered the stream will fail. If so, a char could be used as the type of choice.

This post has been edited by #define: 22 August 2012 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Exception Handling Noob

Posted 22 August 2012 - 04:12 PM

In C++, Exception handling is not a replacement for flow control. Which is what you are doing here.

Exception handling is valuable in trying to deal with the issue of exceptional behavior that cannot be handled easily at the point of failure. Examples are out of memory exceptions. That is not a localized consequence, and needs to be handled higher up the chain of responsibility.

Which means you need a way to unwind from your current position into a stable state, where you can inspect and handle the error. Exceptions make it easy to trigger this unwinding, and stop at a suitable point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1