Changeset dd43a7 for molecuilder/src


Ignore:
Timestamp:
Apr 25, 2010, 1:16:09 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Children:
0707dd
Parents:
65b413
Message:

Made the input methods more robust to invalid inputs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/UIElements/TextDialog.cpp

    r65b413 rdd43a7  
    6262
    6363bool TextDialog::IntTextQuery::handle() {
    64   Log() << Verbose(0) << getTitle();
    65   cin >> tmp;
     64  bool badInput = false;
     65  do{
     66    badInput = false;
     67    Log() << Verbose(0) << getTitle();
     68    cin >> tmp;
     69    if(cin.fail()){
     70      badInput=true;
     71      cin.clear();
     72      cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
     73      Log() << Verbose(0) << "Input was not a number!" << endl;
     74    }
     75  } while(badInput);
     76  // clear the input buffer of anything still in the line
     77  cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    6678  return true;
    6779}
     
    7587bool TextDialog::StringTextQuery::handle() {
    7688  Log() << Verbose(0) << getTitle();
    77   cin >> tmp;
     89  getline(cin,tmp);
    7890  return true;
    7991}
     
    8698
    8799bool TextDialog::DoubleTextQuery::handle() {
    88   Log() << Verbose(0) << getTitle();
    89   cin >> tmp;
     100  bool badInput = false;
     101  do{
     102    badInput = false;
     103    Log() << Verbose(0) << getTitle();
     104    cin >> tmp;
     105    if(cin.fail()){
     106      badInput = true;
     107      cin.clear();
     108      cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
     109      Log() << Verbose(0) << "Input was not a number!" << endl;
     110    }
     111  }while(badInput);
     112  cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    90113  return true;
    91114}
     
    98121
    99122bool TextDialog::MoleculeTextQuery::handle() {
    100   int idxOfMol;
    101   Log() << Verbose(0) << getTitle();
    102   cin >> idxOfMol;
    103   tmp = molecules->ReturnIndex(idxOfMol);
    104   while(!tmp && (idxOfMol !=-1)) {
    105     Log() << Verbose(0) << "Invalid Molecule Index" << endl;
     123  int idxOfMol=0;
     124  bool badInput = false;
     125  do{
     126    badInput = false;
    106127    Log() << Verbose(0) << getTitle();
    107128    cin >> idxOfMol;
     129    if(cin.fail()){
     130      badInput = true;
     131      cin.clear();
     132      cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
     133      Log() << Verbose(0) << "Input was not a number!" << endl;
     134      continue;
     135    }
     136
    108137    tmp = molecules->ReturnIndex(idxOfMol);
    109   }
     138    if(!tmp && idxOfMol!=-1){
     139      Log() << Verbose(0) << "Invalid Molecule Index" << endl;
     140      badInput = true;
     141    }
     142
     143  } while(badInput);
     144  cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    110145  return (idxOfMol!=-1);
    111146}
     
    133168
    134169bool TextDialog::ElementTextQuery::handle() {
    135   int Z;
    136   Log() << Verbose(0) << getTitle();
    137   cin >> Z;
    138   tmp = World::getInstance().getPeriode()->FindElement(Z);
    139   return tmp;
    140 }
     170  bool badInput=false;
     171  bool aborted = false;
     172  do{
     173    badInput = false;
     174    Log() << Verbose(0) << getTitle();
     175
     176    // try to read as Atomic number
     177    int Z;
     178    cin >> Z;
     179    if(!cin.fail()){
     180      if(Z==-1){
     181        aborted = true;
     182      }
     183      else{
     184        tmp = World::getInstance().getPeriode()->FindElement(Z);
     185        if(!tmp){
     186          Log() << Verbose(0) << "No element with this atomic number!" << endl;
     187          badInput = true;
     188        }
     189      }
     190      continue;
     191    }
     192    else{
     193      cin.clear();
     194    }
     195
     196    // Try to read as shorthand
     197    // the last buffer content was not removed, so we read the
     198    // same thing again, this time as a string
     199    string shorthand;
     200    cin >> shorthand;
     201    if(!cin.fail()){
     202      if(shorthand.empty()){
     203        aborted = true;
     204      }
     205      else{
     206        tmp = World::getInstance().getPeriode()->FindElement(shorthand.c_str());
     207        if(!tmp){
     208          Log() << Verbose(0) << "No element with this shorthand!" << endl;
     209          badInput = true;
     210        }
     211      }
     212    }
     213    else{
     214      Log() << Verbose(0) << "Could not read input. Try Again." << endl;
     215      cin.clear();
     216      cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
     217      badInput = true;
     218    }
     219
     220  }while(badInput);
     221  cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
     222  return !aborted;
     223}
Note: See TracChangeset for help on using the changeset viewer.