C programming question

Discussion in 'Game Development General Discussion' started by Zilog Jones, Apr 24, 2005.

  1. Zilog Jones

    Zilog Jones Familiar Face

    Joined:
    Nov 12, 2004
    Messages:
    1,202
    Likes Received:
    0
    I know this is kinda OT, but there seems to be a lot of C programmers here so you may be able to ask my lame-wad question.

    How would I go about putting in error checking to not allow someone to enter any letters when they're supposed to be entering numbers? I'm using pointers, so things seem to go spasticated when someone does try and enter letters.
     
  2. TheDeathcoaster

    TheDeathcoaster Game Developer

    Joined:
    Mar 13, 2004
    Messages:
    1,092
    Likes Received:
    1
    I've only done a bit of C myself...but if you post some of your code (so I can see how your input method works) then I may be able to help :)

    Though Antipasta will probably be able to help you better!
     
  3. AntiPasta

    AntiPasta Guest

    Well, I remember having some major trouble with this too back in Uni's 1st year, but I hope I can help, what library are you using?

    And TheDeathcoaster, something in your MSN name tells me you've been using pointers too much :D
     
  4. subbie

    subbie Guardian of the Forum

    Joined:
    Feb 25, 2005
    Messages:
    4,749
    Likes Received:
    94
    Are you checking on a Character by Character basis? If so thats just a simple compair

    if( (*myPointer >= '0') && (*myPointer <= '9') )
    { /* Valid */ }
    else
    { /* not valid */ }

    if your trying to do something a bit else (like at key press). It helps to know more, you doing a dos or windows app?

    Code helps too. :D
     
  5. kammedo

    kammedo and the lost N64 Hardware Docs

    Joined:
    Sep 24, 2004
    Messages:
    2,138
    Likes Received:
    12
    I mostly use this one here
    http://man.he.net/man3/strtol
     
  6. kammedo

    kammedo and the lost N64 Hardware Docs

    Joined:
    Sep 24, 2004
    Messages:
    2,138
    Likes Received:
    12
    I forgot - obviously you have to scanf the input first to a string which you have malloc'ed before ^^;
     
  7. Zilog Jones

    Zilog Jones Familiar Face

    Joined:
    Nov 12, 2004
    Messages:
    1,202
    Likes Received:
    0
    I'm doing it in DOS, because I is oldskool, or something. I thought about something like the above, but I'm doing a scanf for an integer, which could be more than 9.

    And AntiPasta, I'm not doing C++ ^_^

    strtol looks like it could work, but would make the program a lot more complicated. It's some stupid assignment where I have to make a program that stores records of athletes in a triathlon - it has to use linked lists and I have to be able to edit/delete/export the details to a text file. I've barely done any of those parts yet...
     
  8. AntiPasta

    AntiPasta Guest

    Good :D

    w00t! DOS progging is fun (imho) :smt033

    But what library function are you using for input? I think you at least need to supply that info for us to help you. However, as you're running on DOS, you can easily make your own, using getch() to get raw key inputs, and then checking the scancodes - if they're in the numerical mode, put them in the buffer.
     
  9. subbie

    subbie Guardian of the Forum

    Joined:
    Feb 25, 2005
    Messages:
    4,749
    Likes Received:
    94
    That snip of code is to do character by character, In which case you never will get something out of range if its a number. So if your string is char myInput[2]; You run a for loop on that for the 2 characters using the above bit to make sure there is no text. After done just to a atoi to get a integer number from the string.

    Actualy I am not sure but with using scanf, cant you specify to only get an integer input?
    Try this

    int i = 0; // sorry im a console guy, i am use to clearing vars on creation
    scanf( "%d", &i );


    Your project sounds simple to do. :D
     
  10. Zilog Jones

    Zilog Jones Familiar Face

    Joined:
    Nov 12, 2004
    Messages:
    1,202
    Likes Received:
    0
    It's only my first year of C (I'm in 2nd year - we did Pascal for first year - DON'T ASK!), so I'd say you would find it easy ^_^

    Sorry AntiPasta, I was thinking for some reason that only C++ has libraries. I'm using stdio.h, stdlib.h and conio.h - it's Turbo C++ 3 by the way. I might try the getch() thing, but it's still gonna make things a lot more complicated, especially with the amount of numbers i need to get inputs from. I'll ask my lecturer if I actually need to do this - he said we need to include checking for erronious numbers (e.g. putting in end times that occur before start times) but I'm not sure if we have to go this far.

    Thanks for the help anyway!
     
  11. subbie

    subbie Guardian of the Forum

    Joined:
    Feb 25, 2005
    Messages:
    4,749
    Likes Received:
    94
    POST SOME CODE!!

    Will make you looking like you know C like a pro does.
     
  12. Zilog Jones

    Zilog Jones Familiar Face

    Joined:
    Nov 12, 2004
    Messages:
    1,202
    Likes Received:
    0
    it's just like scanf(%d, whatever)...
     
  13. subbie

    subbie Guardian of the Forum

    Joined:
    Feb 25, 2005
    Messages:
    4,749
    Likes Received:
    94
    ok, hehe.

    Maybe you should change it to a string "%s" and then do a simple loop though it to make sure each character is a valid letter (code example i posted).

    thats a simple way that is basic and will let you verify its valid quickly.
     
  14. WolverineDK

    WolverineDK music lover

    Joined:
    Mar 14, 2004
    Messages:
    5,611
    Likes Received:
    8
  15. Zilog Jones

    Zilog Jones Familiar Face

    Joined:
    Nov 12, 2004
    Messages:
    1,202
    Likes Received:
    0
    Actually, I found out a much easier way to do it. Instead of scanf, I'd do:

    Code:
    fgets(line, sizeof(line), stdin); //line is a string
    sscanf(line, "%d", &num);
    In every place were I previously used scanf, there was some if statement which would either check (i) if the number existed in the linked list, or (ii) if the number was between 0 and 59 (as it is supposed to be a minute or second), so if a letter is now accidentally entered it'll say it's wrong because of one of these. OK, there's a possibility a letter might get converted to a long int between 0 and 59, but it's very unlikely.

    Thanks for your help anyway - I knew just using scanf wasn't the way to go after reading what you guys said.
     
sonicdude10
Draft saved Draft deleted
Insert every image as a...
  1.  0%

Share This Page