Fflush (stdin) and Rewind (stdin)

Source: Internet
Author: User
Tags rewind

#include <stdio.h>#include<math.h>intMain () {DoubleA, B, C, disc, X1, x2, p, q, I;  Do{scanf_s ("A=%LF,B=%LF,C=%LF", &a, &b, &c); I= B*b-4* * *C; }  while(i<0); printf ("Enter the correct"); Disc= B*b-4* * *C; P=-B/(2*a); Q= sqrt (disc)/(2*a); X1= p + q, x2 = P-Q; printf ("x1=%5.2f\nx2=%5.2f\n", x1, x2); return 0;}

In the group of people asked this example, calculated as a two-time equation, scanf () can not be used in vs2015, so I changed it to scanf_s ().

At the time I thought it entered:

A=1,b=2,c=3

does not satisfy its input correct condition, so must enter a=?,b=?,c=? These, however, can not continue to input, press other keys without any reaction, someone reply with fflush (stdin);

#include <stdio.h>#include<math.h>intMain () {DoubleA, B, C, disc, X1, x2, p, q, I;  Do{fflush (stdin); scanf_s ("A=%LF,B=%LF,C=%LF", &a, &b, &c); I= B*b-4* * *C; }  while(i<0); printf ("Enter the correct"); Disc= B*b-4* * *C; P=-B/(2*a); Q= sqrt (disc)/(2*a); X1= p + q, x2 = P-Q; printf ("x1=%5.2f\nx2=%5.2f\n", x1, x2); return 0;}

However, on my vs2015 still can not run, then I go to Baidu query.

The second loop cannot be entered because of the problem of the input buffer, after reading a number or character immediately after the read, should clear the input buffer, and fflush (stdin) can not run in vs2015, mainly because the function function is: empty input buffer, To ensure that the subsequent data reads are not affected. Note, however, that this function applies only to some compilers, such as VC6, and not all compilers support this function. The most appropriate practice is to add rewind (stdin).

#include <stdio.h>#include<math.h>intMain () {DoubleA, B, C, disc, X1, x2, p, q, I;  Do{rewind (stdin); scanf_s ("A=%LF,B=%LF,C=%LF", &a, &b, &c); I= B*b-4* * *C; }  while(i<0); printf ("Enter the correct"); Disc= B*b-4* * *C; P=-B/(2*a); Q= sqrt (disc)/(2*a); X1= p + q, x2 = P-Q; printf ("x1=%5.2f\nx2=%5.2f\n", x1, x2); return 0;}

Or output some of the following before reading:

#include <stdio.h>#include<math.h>intMain () {DoubleA, B, C, disc, X1, x2, p, q, I;  Do{printf ("input A,b,c:"); scanf_s ("%LF%LF%LF", &a, &b, &c); I= B*b-4* * *C; }  while(i<0); printf ("Enter the correct"); Disc= B*b-4* * *C; P=-B/(2*a); Q= sqrt (disc)/(2*a); X1= p + q, x2 = P-Q; printf ("x1=%5.2f\nx2=%5.2f\n", x1, x2); return 0;}

Fflush (stdin) and Rewind (stdin)

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.