Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking for valid entry 1

Status
Not open for further replies.

mThomas

Instructor
May 3, 2001
404
0
0
US
I know this is a common question. I've looked through the FAQ's, done countless searches on the net, looked in my MS Visual C++ .NET book, and yet I still am having a hard time doing what "should" be simple, and that is check to see that a numeric entry is valid. I want the input to be a float type. If the entry is a char, then return to the input and keep looping back until a valid entry is made.

I've tried a do while, while and goto, yet all I get is an infinate loop when I enter a char. Below is the code that has come closest to what I want. If I can just get it to loop, then I'd be ok.

I also tried the ctype functions like isdigit, but so far no luck. *g* This is sure easier in Java, Perl and JavaScript.

Any guidence would be most appreciated.

tia...mike

Code:
#include <iostream>

using namespace std;

int main(void)
{

float number = 0;

cout << "Enter a number: ";
fflush(stdout);

if(!(cin >> number)) 
{
 cerr << "Sorry, that is a non-valid entery" << endl; 

}

return 0;

}
 
Well, if it behaves anything like scanf, it throws its hands up in horror when it finds a char & you get an infinite loop.

I've tended to write stuff that reads the char as a char & rejects non numerics.

Then when the required field is filled, I do a string to numeric conversion.

rgds
Zeit.
 
Try this pattern:
Code:
  double number = 0; // Never use float (it's too short!)

  for (;;)
  {
    cout << "Enter a number: ";
    number = 0.0;

    if (!(cin >> number)) // OK, let's RTL decide...
    {
       if (cin.eof()) // Done...
           break;
       cerr << "Sorry, that is a non-valid entry" << endl;
       cin.clear(); // clear stream fail bit to continue!
       cin.ignore(100,'\n'); // skip bad input in the line.
    }
    else
	cout << "OK " << number << endl;
  }
 
ArkM,

With a slight modification, ie. break in the else and I added a variable for the for so the user is allowed x number of trys.... it works like a charm!!

Even though I can only give you one star, I'm sure this will be the source of many more as others come in search of this sweet solution.

At least I was on the right track. Setting the number = 0.0 would probably never have occured to me :)

thanks....mike
 
Thank you, Mike.
All the best!
 
I noticed that my routine allows for blank entries and multiple decimal values. So I started playing with a switch statement to loop through an input. I can not seem to catch a blank space between numbers or multiple decimials, like 1.1.1 or 1 1. However I would prefer to amend ArkM's suggestions to catch a blank space or multiple decimals?

Here is what else I've been trying? I would prefer to amend the original code I was working on that ArkM got working form me.

Code:
	char num[5] = "";
	short n = 0;
	char chk = 'F';

 CHK = exit flag
	while (chk == 'F')
	{

    
		n = 0;
		char num[5] = "";
		cout << "\nEnter a number: ";
	    cin >> num;


		while ( n < 5 ) 
		{ 
			
			switch ( num[n] )
			{
			case '0': chk = 'T'; break;
			case '1': chk = 'T'; break;
			case '2': chk = 'T'; break;
			case '3': chk = 'T'; break;
			case '4': chk = 'T'; break;
			case '5': chk = 'T'; break;
			case '6': chk = 'T'; break;
			case '7': chk = 'T'; break;
			case '8': chk = 'T'; break;
			case '9': chk = 'T'; break;
			case '.': chk = 'T'; break;
			case ' ': chk = 'T'; break;
			case NULL: break;		 

                        default : chk = 'F';
			                 n = 5;

                        cout << "\n\nSorry, there appears to be a data entry error.";

						break;
			}
			
			n++;
		}

tia...mike
 
Sorry, mThomas, but your approach is incorrect. Avoid using cin >> char_array because of you can't control buffer overflow in that case.
For the full control over input you must implement some kind of a (number) scanner routine. For example, input a line into the buffer std::string (with getline(cin,...) function call) then see what's its contents by hands (char by char). It's a very simple grammatics so it's not so hard (but a very instructive for entry level;) task...
 
Thank you ArkM for your advise. That is exactly what I did. I used the cin.getline(variable, sizeof(variable)) to loop through the variable in a char type. I check each character for what ever I'm looking for. Once I have what I want, I convert to a numeric value using atof() or atoi().

Works like a charm :)

mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top