NumberQuestion.h

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  *
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00039 inline string i2s(int x)
00040 {
00041   ostringstream o;
00042   o << x;
00043   return o.str();
00044 }
00045 
00051 inline int s2i(const string x)
00052  {
00053    istringstream i(x);
00054    int x1;
00055    char c;
00056    if (!(i >> x1) || i.get(c))
00057      FAILURE(Logic,"invalid conversion");;
00058    return x1;
00059  }
00060 
00065 class NumberQuestion: public FreeTextQuestion
00066 {
00067 public:
00071   void setRange(int low, int high) 
00072   { 
00073     mLow = low; mHigh = high; 
00074   }
00075 
00079   int getNum()
00080   {
00081     return s2i(mAnswer);
00082   }
00083 
00087   virtual string ask()
00088   {
00089     /* Set the options list to (low-high).
00090      */
00091     setOptions(i2s(mLow) + "-" + i2s(mHigh));
00092 
00093     /* Keep repeating the question until a valid
00094      * answer is received.
00095      */
00096     for (;;)
00097     {
00098       int value;
00099 
00100       /* Ask the question as a FreeTextQuestion. This
00101        * takes care of the explanation and default
00102        * value handling.
00103        */
00104       FreeTextQuestion::ask();
00105 
00106       try
00107       {
00108         /* Convert the answer to an integer.
00109          */
00110         value = s2i(mAnswer);
00111 
00112         /* If the answer is within the allowed range,
00113          * return it.
00114          */
00115         if ( value >= mLow && value <= mHigh)
00116           return mAnswer;
00117 
00118         /* The answer is out of range.
00119          */
00120         cout << "\nPlease enter a value that is within the indicated range, or '?' for help.\n\n";
00121       }
00122       catch(CLogicException)
00123       {
00124         /* The answer is not a number.
00125          */
00126         cout << "\nPlease enter an integer value.\n\n";
00127       }
00128     }
00129   }
00130 
00131 protected:
00133   int mLow;
00135   int mHigh;
00136 };

SourceForge.net Logo
Project space on SourceForge.net