![]() |
|
#1
|
|||
|
|||
Problem with alpharithmetical checkI recently made a calculating program in C but in order to improve it i want to make a check on the values that the user enters.It's not the following but this simple code also reflects my problem.
CPP / C++ / C Code:
I was told to use functios atoi from math.h and fgets or isalpha from ctype.h , but i don't know how. Any suggestion would be helpful.Thanks in advance. :-) Last edited by JdS : 09-Dec-2004 at 16:52.
Reason: Please insert [c] & [/c] tags between your example C codes
|
|
#2
|
||||
|
||||
|
Quote:
Hello and Welcome to GIDForums absent. A few things you want to do. READ this thread about the code tags. To do that just make it look like this in your editor. Quote:
That will maintain your formatting and use the custom code highlighter that folks here are proud to have at their disposal. It helps you get more responses as well. Quote:
Here is a link to a post here with some good info (at least I think so) that touches on the topic of input. My last post shows how to use cctype to filter the input before attempting to use it. Last but not least read this thread that talks about scanf along the way but more importantly describes in depth why your problem is occurring. My example is c++ but I modified most from c code and the headers that are eventually included in my example are the same ones you want to use. There are also some helpful reference links at the bottom of the post. Hope this was helpful. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#3
|
|||
|
|||
|
Quote:
The way preferred by most experienced programmers includes fgets(), followed by sscanf(), atoi() or some such thing. For starters, however it is possible, and maybe even practical, to use scanf(). Here's an example. If you want to use scanf() for numeric input, you should always test to see if the scan succeded. scanf() returns the number of items that it saw. When scan is looking for a numeric entry, if you enter a non-numeric character, scanf() stops without doing anything else, so the character is still in the input buffer for the next scanf() or getchar() or whatever. Unlike C++ use of cin>>, scanf() input anomalies are fairly easy to recover from. Try this: CPP / C++ / C Code:
Try it with inputs like 123 12a abc etc. Regards, Dave |
|
#4
|
|||
|
|||
|
i liked your idea davekw7x
but your code is causing a segment i mean it doesn't give the user the chance to answer again. I didn't understand the part with the printf("(0x%02x hex). ***\n", number); but i hadn't thought using scanf inside if thanks anyway. |
|
#5
|
|||
|
|||
|
Quote:
Oh, man!!! I try really hard to post code that works. There was a serious error that the compiler should have caught. Here's something better (I hope). CPP / C++ / C Code:
Somehow, in the code that I posted, the "eat the bad character" line sneaked to an illegal spot (between the if{} and else{} stuff.) Regards, Dave |
|
#6
|
|||
|
|||
|
That's it man, thanks!!
|
|
#7
|
|||
|
|||
|
Quote:
For debug purposes I wanted to print out the illegal character. If the character was printable, I printed it as %c. If the user entered something not printable, say Ctrl-A, I printed the hex value of the character (0x01 in this case). Regards, Dave |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD burner will not burn, new problem | sdshaman | Computer Hardware Forum | 4 | 06-Feb-2008 23:17 |
| Gallery Thumbnail problem | Darksat | MySQL / PHP Forum | 2 | 25-Oct-2004 08:21 |
| Debugging problem.... | Max | C Programming Language | 4 | 04-Aug-2004 04:59 |
| C I/O problem | kelly80 | C Programming Language | 4 | 27-Apr-2004 20:15 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The