SCANF control the legal range of user input data _ algorithm _c language

Source: Internet
Author: User

Title:

Requires a a,b,c three integer variable to store the user input 3 values.

See Program implementation:

#include <stdio.h>intMain () {intA =0, B =0, C =0;  while((A * b *c <1&& (a<1|| b<1|| c<1)) && (scanf ("%d%d%d", &a, &b, &c) <=3) {fflush (stdin);    }; printf ("%d%d%d\n", A, b, c); return 0;}
The scanf function returns a value of type int. If both A and B are read successfully, then the return value of the scanf is 2, and if only a is read successfully, the return value is 1, if both A and B are not successfully read, the return value is 0, and if an error is encountered or an end of file is encountered, the return value is EOF. End of file is CTRL + Z or Ctrl+d

Flexible use of the return value of the scanf can be more efficient with the data entered by the user, and will not cause the entire program to run because of an error in the input.

Use the while loop to get user input and get the data again when the condition is not met. Until the data entered by the user is valid and then stopped,

In a cyclic read, use Fflush (stdin) to flush the standard input buffer and discard anything in the input buffer.

Note: A good algorithm design to ensure the robustness of the program, and sometimes can be in the case of defective data can still give the correct results.

SCANF control the legal range of user input data _ algorithm _c language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.