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'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!
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
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.
I forgot - obviously you have to scanf the input first to a string which you have malloc'ed before ^^;
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...
Good 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.
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.
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!
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.
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/
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.