Hello, this is my first post so be nice :)

After hours of trolling various forums and websites along with a few books i've decided to ask for help. I've put together the whole of the program which is to carry out a few simple processes using inputted data and display the number in word form (i've done that bit).

The problem is when im testing on extronus data (in the case letters) it hits an infine loop. So i would like to have validation in to stop this from happening, as i have done with the values if they're too big or too small.

From current research i've found isdigit and using:

  cin.ignore(numeric_limits<int>::max(), '\n');
  if(!cin || cin.gcount() != 1);

with no luck. I'll paste a function out of my code and if you could point me in the right direction that would be perfect. I've got all the correct libaries and used all the correct libaries when attempting to use the last two. Also any varibles involved in this function are global.

void inputweeks(){
     while(weeks < 4 || weeks > 5)
     {cout << "Enter amount of weeks in the month (4 or 5): ";
     cin >> weeks;
     if(weeks < 4 || weeks > 5)
     {cout << "!ERROR!! this is an inncorrect input" << endl;
     }}
}

Kind Regards

A

use a loop asking for input, stopping only when input is valid.
Within the loop use the >> to attempt input into a numeric variable.
check validity of the istream after the attempt. If it is in the fail state then assume the input was invalid and call the istream clear() followed by the istream ignore() method to clear out the istream buffer.

Or, instead of checking the istream status after attempted input, never accept input into a numeric variable. Only accept input into a string. Then parse the string looking for things that shouldn't be there such as a string that's 80 digits long or a string with non digit char or too many decimal points or some char other than a single +, -, e, E, and maybe ^. Then, when you have validated all char and combination of char convert the string into the numeric value of choice using whatever method you want: eg and isstrinstream and the >> operator or strof() or atoi() or whatever.

ok, thanks for the help. I've amended my code to this.

void inputweeks(){
     while(weeks < 4 || weeks > 5)
     {cout << "Enter amount of weeks in the month (4 or 5): ";
     cin >> weeks;
     {if(!(weeks));
     cin.clear();
     cin.ignore(numeric_limits<streamsize>::max());
     (cout << endl << "!Error!! Please enter a number between 4 and 5 " << endl);}
     }
}

It works! with a slight side effect. It prints an error even if the while statement is false (i think i got that right). The point is it prints an error message all the time instead of just when an integer of <4 or >5 or a letter is entered.

Any ideas and then its fixed :D

P.s. thanks for you input lerner, looked into some of the things you said and came back with what is above.

bool invalid = true;
while(invalid)  
{
   cout << "Enter amount of weeks in the month (4 or 5): ";
  cin >> weeks;
  if(cin.fail())
  {
     cin.clear();
     cin.ignore(numeric_limits<streamsize>::max());
     cout << endl << "!Error!!  Non-numerical input.  Try again." << endl;
  }
  else if(weeks < 4 || weeks > 5)
  {
     cout << "Error.  Input not in valid range.  Try again" << endl;
  }
  else
  {
     invalid = false;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.