问题
#include <stdio.h>
int main(){
float x,y;
printf("Enter 2 numbers: \n");
scanf_s("%f %f", &x, &y);
if (getchar() == '\n')
{
printf("Division: %f\n", x / y);
printf("Product: %f\n", x * y);
printf("Sum: %f\n", x + y);
printf("Difference: %f\n", x - y);
}
else
{
printf("no Float-Value!");
}
getchar();
return 0;
}
we need to get a loop in so if we enter the wrong format the program should ask again to enter two numbers
回答1:
The best way of checking the validity of the input is to check the return value of scanf_s
which tells you the number of variables that were successfully set. In your case, if it's 2 then all is well; otherwise you need to repeat.
In other words
do {
int c;
while ((c = getchar()) != '\n' && c != EOF); // clear the input stream
printf("Enter 2 numbers: \n");
} while (scanf_s("%f %f", &x, &y) != 2);
is an appropriate control structure, rather than if (getchar() == '\n')
, which you should drop.
回答2:
What I don't like about using scanf
family of functions is that it will go berserk and crash your program when you input something wrong. For example, if you have scanf("%f", &a)
, try inputing stack overflow
. It goes nuts!
So, as I said in the comments, I think you should build your own validating function, and get user input as just a string using fgets
.
Here is a simple code that will get you started in it. This is very ugly code and you should refactor it.
#include <stdio.h> /* printf, fgets */
#include <ctype.h> /* isdigit, isspace */
#include <string.h> /* strlen */
int is_valid_float(char *s, int len)
{
int i;
for(i = 0; i < len; i++) {
/* floats may have a decimal point */
if(s[i] == '.') continue;
/* spaces should be ignored, since they separete the nubmers */
if(isspace(s[i])) continue;
/* is there's anything besides a number, we abort */
if(!isdigit(s[i])) return 0;
}
/* if we got here, then the input contains two valid floats.
*
* Does it? Test it!
*/
return 1;
}
int main(void)
{
float a, b;
char buf[100];
int len;
do {
fprintf(stderr, "Enter A and B: ");
fgets(buf, sizeof buf, stdin);
/* fgets will store a newline at the end of the string, we
* just overwrite it with a null terminator
*
* Thanks to @chux for the strcspn() suggestion.
*/
buf[strcspn(buf, "\n")] = 0;
} while(!is_valid_float(buf, len));
/* now, after we know the string is valid and won't cause scanf to go
* berserk, we can use it in our validated string.
*
* Here is where you should really take a moment and look at the
* validation function and improve it. Not valid strings will cause
* your program to go nuts.
*/
sscanf(buf, "%f %f", &a, &b);
/* Did it scan the numbers correctly? */
printf("A: %f\n", a);
printf("B: %f\n", b);
return 0;
}
来源:https://stackoverflow.com/questions/50840130/how-to-loop-scanf-s-until-success