c - 我似乎无法理解如何将我的 scanf 限制为仅 float

标签 c

    #include <stdio.h>
    #include <string.h>
    #define mL 5
    #define NL 20
    #define UL 6



    struct LIST
    {
        char n[NL];
        float am;
        char u[UL];
    };
    struct array
    {
        struct LIST array;
    };


    void addCityInformation(struct array *add, int *items);
    void printCities(struct array *all, int items);
    int main(void)
    {
        struct array shopping[mL];
        int choice, nrOfItemsAdded = 0;

        do
        {
            printf("\nWhat du you want to do?");
            printf("\n1 - add grocery");
            printf("\n2 - print shopping list");
            printf("\n3 - exit");
            printf("\nYour choice: ");
            scanf("%d", &choice);
            while(getchar() != '\n');

            switch (choice)
            {
            case 1:
                addCityInformation(&shopping[nrOfItemsAdded], &nrOfItemsAdded);
                break;
            case 2:
                printCities(shopping, nrOfItemsAdded);
                break;
            case 3:
                printf("Exiting program\n\n");
                break;
            default:
                printf("Invalid input\n\n");
                break;
            }
        }
        while(choice != 3);
        return 0;
    }

     int clean_stdin()
{
    while (getchar()!='\n');

}

    void addCityInformation(struct array *add, int *items)
    {

        if(*items == mL)
            printf("No more space in the list\n");
        else
        {
            printf("Enter name: ");
            fgets(add->array.n, NL, stdin);
            add->array.n[strlen(add->array.n)-1] = '\0';
            do {

        printf("Enter amount: ");
        }while (scanf("%f", &add->array.am )); //loop untill other than float 
            getchar();
            printf("Enter unit: ");
            fgets((add->array.u), UL, stdin);
            add->array.u[strlen(add->array.u)-1] = '\0';
            (*items)++;
        }
    }

    void printCities(struct array *all, int items)
    {

        printf("\n\n%-20s %-15s %-9s | %-6s\n", "Name", "amount", "unit");
        printf("--------------------------------------------------------\n");
        for(int i = 0; i < items; i++)

            printf("%-20s %-15.1f %-9.4s \n", all[i].array.n, all[i].array.am, all[i].array.u);

    }

这是我的循环,除此之外我只显示了代码的一部分。现在它只是继续提供输入金额并让我将其注册到结构中。我想限制用户只能输入正数而不输入任何字符。如果他输入一个字符,即使它是 123Av123,它也应该重新运行循环,它应该运行循环并只注册正确的数字

编辑:现在显示整个代码//循环直到我想要帮助而不是 float

最佳答案

int check=scanf("%f", &add->array.am )
if(check!=1||add->array.am<0){
printf("Incorrect input");
return 1;
}

我想这样就可以了。 编辑:您希望它在之后重新运行,因此使用 continue; 而不是 return;

关于c - 我似乎无法理解如何将我的 scanf 限制为仅 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58682963/

相关文章:

c - 使用递归在 C 中进行二进制搜索

c - Ruby (2.0) c 扩展从 c 函数返回数组数组

C - 保持打印终端输出到位

c - 如何将 plplot 图形嵌入到 ncurses 中?

c - 如何实现删除节点的功能?

C:AF_UNIX 套接字上的剩余字节

c - 如何使用c语言将.csv文件中的数据读取到多维数组?

C malloc 和自由函数的奇怪行为

c - 显示按状态过滤的进程的系统调用

c - 对这一灾难性声明的解释正确吗?