如何循环scanf_s()直到成功?

时间:2018-06-13 14:45:39

标签: c loops while-loop format

#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;
}

我们需要一个循环,所以如果我们输入错误的格式,程序应该再次要求输入两个数字

2 个答案:

答案 0 :(得分:5)

检查输入有效性的最佳方法是检查scanf_s的返回值,它告诉您成功设置的变量数。在你的情况下,如果它是2那么一切都很好;否则你需要重复。

换句话说

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);

是一个合适的控制结构,而不是if (getchar() == '\n'),你应该放弃它。

答案 1 :(得分:1)

我对使用scanf系列功能感到满意的是,当您输入错误信息时,它会变得狂暴并导致程序崩溃。例如,如果您有scanf("%f", &a),请尝试输入stack overflow。它疯了!

所以,正如我在评论中所说,我认为您应该构建自己的验证函数,并使用fgets将用户输入作为字符串。

这是一个简单的代码,可以帮助您入门。这是非常难看的代码,你应该重构它。

#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;
}