Really when this article weigh yourself do not know its title should write out why ... "Check the number you enter there is some"@@Slightly funny because some have called then check what to do, or is "Check the number of C" - well not quite ready for the purposes of this article. And finally decided to put as above for the purpose of entering into by scanf will know immediately that there must be a number or not.
The program will perform the following request you to enter an integer (int), if you enter the wrong number (whether any string format "12dabc", "abc123" and even made some 12.3) they are caught you in again.
#include <stdio.h> #include <stdlib.h> int clean_stdin () {while (getchar ()! = 'n') {} return 1; } Int main (void) {int input; char c; do {printf ("Enter an integer:"); } While (((scanf ("% d% c", & input, & c)! = 2 || c! = 'N') && clean_stdin ())); printf ("done, number is% d", input); return 0; }
scanf ("% d% c", & input, & c) This will allow us to enter into an integer and a character that immediately follows an integer, and it returns an integer - number turn it receives (scan) is in accordance with the format in the format string. - If you enter abc, the function returns a value of 0 because it can not read any suitable value. karenmillen - If you enter 123 and press Enter, the return value is 2 2 value that is acceptable format, and enter 123 for% d for% c. - If you enter 123abc and press Enter, the return value is 2 2 value that is acceptable format, and 123 for% d% c for a character.
scanf ("% d% c", & input, & c)! = 2 || c! = 'n' It ensures that the amount of data that scanf scans are not as 2 or letters received not enter. Put this condition in the while to the machine can perform properly karenmillen if you do not have to enter the right number and then enter it will repeat.
(Scanf ("% d% c", & input, & c)! = 2 || c! = 'N') && clean_stdin () function clean_stdin () is the function always returns a value of 1 (which is always true) it's Mansion the deletion of the buffer (stdin) by reading the entire character left in the buffer (the character that scanf can not read the formatting of it) until the reader to enter characters. The conditions put it in a while loop to wipe cache every time you enter the wrong to re-enter service next time.
Posted in LT C - C ++ | Tags: catch enter in C, check number in C, test string is zero in C, check the input string karenmillen can be zero, check the entered numbers in C, check enter the correct number
Additional Reading ... C Programming: Problem 16 - Build the stack (Queue) Programming C: Last 15 - Installing stack (Stack) C Programming: Problem 14 - doubly karenmillen linked list C Programming: Last 13 - List installed applications linked by pointers Programming C: Unit 12 - List of links set in array
You can use these HTML tags and attributes the following: <a href="" title=""> <abbr title = ""> <acronym title = ""> <b> <blockquote cite = ""> <cite karenmillen > <code> <del karenmillen datetime = ""> <em> karenmillen <i> <q cite = ""> <strike> <strong>
Select language Vietnamese Translate blog / 2014/05/27 / cc-kiem-tra-chuoi-nhap-bang-scanf-co-la-over karenmillen / English / 2014/05/27 / cc-kiem-tra-chuoi-nhap tape-scanf-co-la-than /? lang = en 日本語 / 2014/05/27 / cc-kiem-tra-chuoi-nhap-bang-scanf-co-la-than /? lang = ja 中文 (简体) / 2014/05/27 / cc-kiem-tra-chuoi-nhap-bang-scanf-co-la-than /? lang = zh
New posts Learner karenmillen Android Programming C Programming: Problem 16 - Build the stack (Queue) Programming C: Last 15 - Installing stack (Stack) C Programming: Problem 14 - doubly linked list
Categories Select Category Life is colorful (20) Programming (166) LT Android (22) LT Assembly (11) LT C - C ++ (48) LT C # (2) LT Java (61) LT Java Swing (23) LT Pascal (14) Linux (59) LT object (1) School of PP scientific research (3) School karenmillen Computer Graphics (7) Algorithms (45) TT Trees (4) No action planning (4) Spoj TT (11) TT Graphs (3) Wordpress.com (14) Z Other Categories (19)
Last viewed karenmillen [School_PPNCKH] System methods of scientific research [algorithm] Find the shortest path Dijkstra, Floyd [Linux - C / C ++] Programming C / C ++ on Ubuntu (Linux) [Linux] Installing ibus -bogo type in Vietnamese on Ubuntu 14:04 78