Thread: Validating data Input

  1. #1
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57

    Validating data Input

    I was just wondering on how to validate data in visual c++ (creating a calculator where user type the two numbers in two different text boxes). I know how to do it in c++ but it seems a bit more difficult in GUI. At the moment, i'm reading a input from a text box and converting the string into a int.

    I don't have the code here at the moment, as I am at uni at the moment and quickly typing this before I attend one of my lectures.

    Basically, i want to be able to test the data is digit, (i was doing some research, but somehow isdigit() doesn't work) and within the int range.. ie. 2.2e-300 to 2.2e303...I'll probably post my code tonite when i get home to show what i have done so far.

    One thing, in my 1'st project, which was done in c++, u guys know why when i did after assinging just say 2.2e400, and when i test to see if it is within range i.e if(int < 2.2e-300 || int > 2.2e300), i put up a message. but somehow it doesn't work? i'll provide the code tonite as i said.

    Thanks, gotta go...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    bool ValidDigit(char Digit)
    {
      if(Digit == '.') return true;
      if((Digit >= '0') && (Digit <= '9')) return true;
    
      return false;
    }
    
    bool ValidNumber(const std::string& Input)
    {
      std::stringstream Stream(Input);
      double Value;
    
      for(int i = 0; i < Input.size(); i++)
      {
        if(!ValidDigit(Input[i])) return false;
      }
    
      Stream >> Value;
    
      return (Value >= 2.2e-300) && (Value <= 2.2e303);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > bool ValidNumber(const std::string& Input)
    Except "123.456.789" would be regarded as valid.

    Try using strtod(), check errno to see if there was a conversion error, and check endptr to see if it got to the end of the number properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's an example of using strtod(). It seems to work for values from 1e-300 to 1e300. I goes a little freaky if I input 1e1000000000 or something similarly small, but that's probably because I don't know how to handle HUGE_VAL or use errno, etc.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    const int MAX = 1000;
    
    int main()
    {
      char input[MAX]; 
      char *p;
      double result;
      bool invalid = true;
      while(invalid)
      {
    	cout << "Please enter a number : ";
    	cin >> input;
      
    	result = strtod(input, &p); 
    	
    	 //if user pushed the enter key before other input, input is invalid
    	 //if value of p is newline char or null char then all char entered by user were valid.  
    	 if(input[0] != '\n' && (*p == '\n' || *p == '\0'))
    	{
    	  cout << result <<  " is valid input" << endl;
    	  invalid = false;
    	}	 
    	else
    	  cout << "Invalid input." << endl;
      }	
    	  
      cout << endl;
      char ch;
      cin >> ch; 
      return 0;
    }
    Note: I assume you know that if the input in the GUI isn't in the form of a Cstyle string, you will need to convert it to a C style string in order to use strtod().
    You're only born perfect.

  5. #5
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    if i have done my research properly, isidigit() only does one character....how would u do it if someone typed 842390jej, and this is converted to integer...how do u check that? i was trying with a for loop but i don't think it works properl, cos u can't do it as not array? sorri i'm only a beginner

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    char input[] = "842390jej";
    char *endp;
    long int res = strtol( input, &endp, 10 );
    If all is well, you would expect *endp to be either a space or a '\0'
    In this case, *endp would be 'j'.
    Whether this is an error as far as you're concerned is up to you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM