only accept numeric data

This is a discussion on only accept numeric data within the Windows Programming forums, part of the Platform Specific Boards category; k, so i have this single line edit box and i want to get data from the user, but i ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    only accept numeric data

    k, so i have this single line edit box and i want to get data from the user, but i want no alpha data, purely numeric. for some reason, my big long if statement thats supposed to tell if the data is numeric or not - isn't working. idky...its probably a stupid reason, but here's what i'm trying:
    Code:
    for (i = 0; i <= length; i++)
    {
        if (dubbuf[i] < 48 && dubbuf[i] != 46 || dubbuf[i] > 57 || dubbuf[i] != 0)
        {
           debug[0] = dubbuf[i];
           MessageBox(hwnd, debug, "the culprit..", MB_OK);
           go = 0;
        }
    }
    it displays every character (saying that no character it has is a number) in a messagebox and then tells me that i only accept numberic data. the data i've tried entering is 212 , thats purely numeric
    is there a better way? what am i doing wrong? help lol

    also, it would be better if i could disallow alpha values while the user is entering data, i'd much rather do it that way then checking after the person pushes a button. can i do this? how pleeeaaase thanks .
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well the strange mix of && and || in the expression is odd.

    And using something like
    if (dubbuf[i] < '0'
    Would be more readable - what's 46 (without reaching for your ascii table?)

    Personally, I'd use say strtol() and see how far it gets through the string.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol true, 46 is . but i only know that b/c i'm using it.

    i'll have to read up a lil on strtol, but thanks


    mm..also, every time i use atof, instead of having 212.000... i get 212000... , i have a double declared for what atof is putting the value into, and for the char it doesn't matter if i put 212 or 212.0, it always does this.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>single line edit box...i want data, purely numeric<<

    Create the edit control with the ES_NUMBER style bit set.

    To retrieve the value use GetDlgItemInt.

    An EN_CHANGE notification is sent each time a change is made to the edit control's contents.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    why thank you its beautiful.
    if alpha data is entered, GetDlgItemInt returns FALSE and i send a messagebox and do no further processing BUT this is doing something really strange. for debugging purposes, i was outputting the calculations the program made to a text file. then when i finally got it to work, ..i just dont understand what happens. like if it does a calculation from fahrenheit to celsius, i have fiveninths declared as a double and set to: 0.5555555555555. so it does input = fiveninths * (input - 32.0); input is also a double. when the calculation is output to the text file, its 100.000000 but when its sent to the screen, SetDlgItemInt(hwnd, ID_LABEL5, input, TRUE); it outputs 99. i tried FALSE for the signed / unsigned bool but that made no difference, still 99. why??


    //edit - when having the edit declared with ES_NUMBER, it disallows a decimal point
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    try a cast

    SetDlgItemInt(hWnd,ID_CONTROL,(int)input,FALSE);

    also if you need decimals try

    isdigit(dubbuf[i]) //tests char is a digit

    and
    dubbuf[i] == '.' //tests for asii value of a period
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i wasn't using chars anymore as of now but i'm thinking if i need decimals i'm going to have to. even tho GetDlgItemInt is just so much easier, lol *dammit*
    but yea - typecasted or not, it still outputs 99 to the screen and 100.000000 to the text file. here's my function so far:
    Code:
    void Calculate(HWND hwnd, double input)
    {
         FILE *fileptr;
         
         fileptr = fopen("output.txt", "wb");
         if (fileptr == NULL) MessageBox(hwnd, "there was an error creating the file.", 
         "error", MB_OK | MB_ICONERROR);
         
         if (type_of_conversion == 1)
         {
              input = fiveninths * (input - 32.0);
              fprintf(fileptr, "FtoC result: %f\n", input);
              SetDlgItemInt(hwnd, ID_LABEL5, (int)input, FALSE);
             
              fclose(fileptr);
         }
    }

    also - should the last parameter of the SetDlgItemInt be TRUE or FALSE? thanks


    //edit - this is curious. the fraction 5/9 = .55555... and 9/5 = 1.8000... but when put into the function, produce different results. with the code as it is above, it displays 99 on the screen. but if the math changes to:
    input = (input - 32.0) / ninefifths; then the displayed result on the screen is 100.
    *doesn't really get it* whatever.
    i'm still looking to get decimals because you can't really convert a temperature when the only way to do it is with whole degrees. its just not accurate.
    thanks
    Last edited by willc0de4food; 08-19-2005 at 04:00 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM

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