Results 1 to 15 of 15

Thread: C programming question

  1. #1
    Member Hardcore
    Zilog Jones's Avatar

    Join Date
    Nov 2004
    Location
    Limerick, Ireland
    Posts
    1,201

    C programming question

    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.
    Turn after the SECOND CAKE!

  2. #2
    Game Developer Member Hardcore
    TheDeathcoaster's Avatar

    Join Date
    Mar 2004
    Posts
    1,092
    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!
    I agree to all auction Guidlines. Still.

  3. #3
    AntiPasta
    Guest AntiPasta's Avatar
    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. #4
    Super Moderator Staff


    Tribuni Angusticlavii
    subbie's Avatar

    Join Date
    Feb 2005
    Location
    Toronto, ON
    Posts
    4,530
    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. #5
    and the lost N64 Hardware Docs ASSEMbler Hardcore
    kammedo's Avatar

    Join Date
    Sep 2004
    Location
    Ladinia
    Posts
    2,101
    Quote Originally Posted by Zilog Jones
    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.
    I mostly use this one here
    http://man.he.net/man3/strtol
    -------->>>>> YOUR HELP COUNTS : www.amiscdlaladinia.info <<<<<<<<<<<------------------

    "Dai diamanti non nasce niente, dal letame nascono i fiori" - Fabrizio de Andrè

    On 1000 and heading to 10000....

    Oh, BTW : Computational power is obvious today. Long-lasting gameplay not anymore.

  6. #6
    and the lost N64 Hardware Docs ASSEMbler Hardcore
    kammedo's Avatar

    Join Date
    Sep 2004
    Location
    Ladinia
    Posts
    2,101
    Quote Originally Posted by kammedo
    I mostly use this one here
    http://man.he.net/man3/strtol
    I forgot - obviously you have to scanf the input first to a string which you have malloc'ed before ^^;
    -------->>>>> YOUR HELP COUNTS : www.amiscdlaladinia.info <<<<<<<<<<<------------------

    "Dai diamanti non nasce niente, dal letame nascono i fiori" - Fabrizio de Andrè

    On 1000 and heading to 10000....

    Oh, BTW : Computational power is obvious today. Long-lasting gameplay not anymore.

  7. #7
    Member Hardcore
    Zilog Jones's Avatar

    Join Date
    Nov 2004
    Location
    Limerick, Ireland
    Posts
    1,201
    Quote Originally Posted by subbie
    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
    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...
    Turn after the SECOND CAKE!

  8. #8
    AntiPasta
    Guest AntiPasta's Avatar
    Quote Originally Posted by Zilog Jones
    And AntiPasta, I'm not doing C++ ^_^
    Good :D

    Quote Originally Posted by Zilog Jones
    I'm doing it in DOS
    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. #9
    Super Moderator Staff


    Tribuni Angusticlavii
    subbie's Avatar

    Join Date
    Feb 2005
    Location
    Toronto, ON
    Posts
    4,530
    Quote Originally Posted by Zilog Jones
    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.
    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 );


    Quote Originally Posted by Zilog Jones
    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...
    Your project sounds simple to do. :D

  10. #10
    Member Hardcore
    Zilog Jones's Avatar

    Join Date
    Nov 2004
    Location
    Limerick, Ireland
    Posts
    1,201
    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!
    Turn after the SECOND CAKE!

  11. #11
    Super Moderator Staff


    Tribuni Angusticlavii
    subbie's Avatar

    Join Date
    Feb 2005
    Location
    Toronto, ON
    Posts
    4,530
    POST SOME CODE!!

    Will make you looking like you know C like a pro does.

  12. #12
    Member Hardcore
    Zilog Jones's Avatar

    Join Date
    Nov 2004
    Location
    Limerick, Ireland
    Posts
    1,201
    it's just like scanf(%d, whatever)...
    Turn after the SECOND CAKE!

  13. #13
    Super Moderator Staff


    Tribuni Angusticlavii
    subbie's Avatar

    Join Date
    Feb 2005
    Location
    Toronto, ON
    Posts
    4,530
    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. #14
    music lover ASSEMbler Extreme
    Never Logs Out
    WolverineDK's Avatar

    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    5,544
    Zilog Jones:

    since i know you are a dos fan i will post some maybe cool dos links to you :-)

    Bootdisk.Com

    http://www.bootdisk.com/

    DJGPP( a C compiler)

    http://www.delorie.com/djgpp/

    Interesting DOS programs - Home Page

    http://www.opus.co.tt/dave/

    http://www.drdos.com/
    Did you know that sex is illegal without a condom in the state of Nevada ?
    "Historien lærer os, at folk aldrig har lært noget af historien" - G.W.F. Hegel

    "history teach us, that nobody learns from history" G.W.F. Hegel

    Language Learning Resources

  15. #15
    Member Hardcore
    Zilog Jones's Avatar

    Join Date
    Nov 2004
    Location
    Limerick, Ireland
    Posts
    1,201
    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);[/code]

    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.
    Turn after the SECOND CAKE!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •