edit control - testing input

This is a discussion on edit control - testing input within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I've made MFC application that performs calculations with numbers entered from edit controls. For example: Code: ... CString var1,var2; ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    edit control - testing input

    Hi, I've made MFC application that performs calculations with numbers entered from edit controls.
    For example:
    Code:
    ...
    CString var1,var2;
    double var1double,var2double;
    ...
    GetDlgItem(IDC_FS)->GetWindowText(var1);
    GetDlgItem(IDC_VF)->GetWindowText(var_2);
    var1double=atof(var1);
    var2double=atof(var2);
    ...
    Problem arises when user enters some characters or string
    when he's supposed to enter number.
    For example value 5.52 is expected and user enters "dfsd". Function atof() will work and I'll get some dummy values in further calculations.
    My question is how to check if value that is entered (in form string) really number or not?

    Is there any way to check what is entered and to notify user to try again if number is not enetered.
    Thanks for help!

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    use the isdigit function and test the string, something like so:

    Code:
      int length=strlen(buff);
      for(int a=0;a<length;a++)
      {
      	 if(!isdigit(buff[a]) && buff[a]!='.')
      		  break;
      }
      if(a!=length)  // Quit prematurely....must contain invalid characters
      {
      // do stuff
      }
    mrrk, that code's written in C, but you should be able to figure out the C++ equivalent for your code

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Thanks dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 10:52 PM
  3. Problems with my edit control...
    By tyouk in forum Windows Programming
    Replies: 19
    Last Post: 10-19-2003, 01:36 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM
  5. Input Control
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2002, 06:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21