Hi to all!
Do you know how to accept only integer and if the user enters a character, the inputted character will not appear, instead the program will display an error message.
Hi to all!
Do you know how to accept only integer and if the user enters a character, the inputted character will not appear, instead the program will display an error message.
An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse
accept the keyboard input as a string then validate each character of the string. There is no standard C function to get just one charcter from the keybord at a time. But if you are not concerned about ascii C, then use functions in conio.h if your compiler supports them.
I dont know if you can use:
_getch() and _putch();
But look into them.
Code:if( isfriend( isdigit ) ) { printf("isdigit is your friend!\n"); }
Quzah.
Hope is the first step on the road to disappointment.
You could also check the return value of scanf() if you want to input a number.
BTW, isdigit() is in <ctype.h>.
There is no standard C function to get just one charcter from the keybord at a time.Nothing was said about one character. I think Alexthunder wants isdigit() or scanf()._getch() and _putch();
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
By the way, isfriend() is not an ANSI function. One possible implementation of it could be:Originally Posted by quzahCode:int isfriend(int(*func)(int)) { return (func == isdigit); }
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Thanks, I needed a bit of cheering up, after how angry someone here was making me by you know, handing out answers to homework again...
Quzah.
Hope is the first step on the road to disappointment.
Thanks for the replies. Anyway in the
is func a variable that you can use in scanf?Code:int isfriend(int(*func)(int)) { return (func == isdigit); }
An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse
No, that was a joke. It's a function pointer.
Go look up the 'isdigit' function, and you'll see that you pass it a character you want to test, and it tells you if it's a digit or not.
Quzah.
Hope is the first step on the road to disappointment.
Buffered input, etc. Keyboard != stdin.Originally Posted by kermit
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Re-read the original question. If the user presses a non-digit key he wants to display an error message. None of the functions in stdio.h will allow that functionality. Sure, scanf() will stop reading from the keyboard when the first non-digit key is pressed but that is not the behavior needed by the op's question. getchar() will return just one character -- but you have to press <Enter> which makes two keystrokes, not one. In 32-bit programs conio.h contains non-standard getch() that will do what the op needs, but as we all know not every compiler will support that function.Originally Posted by kermit