我想阻止 float 变量存储 . "dot" 和任何负数本身,因为这会在稍后创建一个值,即 -10000000

I want to stop a float variable from storing a . "dot" and any negative numbers by itself as this creates a value later on that is -10000000

我试图寻找方法来设置它以使其停止一个点。从 scanf_s 输入设置为 float 变量,但我没有发现任何东西,这意味着客户可以只输入一个 .点或句号,然后按回车键,程序就可以正常运行了。我想让它失败我试图通过做类似

的事情来欺骗它
if(priceoforange == .){
        printf("Failure);
    }

当然,我尝试了所有可能的输入方式,无论是 = 还是 == 。 , '.', ".", .等等,如果它想要 .成为一个 char 而它被保存在一个变量 float 中,它会让程序做它的事情并设置变量的值,该点被随机放入 -1000000435。

代码是这样的

float priceoforange;

printf("\nWhat would you like the cost of orange to be?  %c", ch); 

scanf_s(" %f", &priceoforange);

这允许将 priceoforange 设置为 .

#include<stdio.h>

int main()
{
    float priceoforange, priceofapple, priceofpear, budget;
    int choice; //Customer choses what item he wants to buy
    char ch = 156; 
    
    printf("--------|Shop Owner Section|--------\n");
    printf("Current items in shop are 1-orange, 2-apple and 3-pear\n");

    printf("\nWhat would you like the cost of orange to be?  %c", ch); 
    scanf_s(" %f", &priceoforange); //sets the cost of orange to input
    printf("What would you like the cost of apple to be?   %c", ch);
    scanf_s(" %f", &priceofapple);

    printf("What would you like the cost of pear to be?    %c", ch);
    scanf_s(" %f", &priceofpear);
    

    printf("-----|End of Shop Owner Section|----\n");
    printf("\nWelcome to the fruit shop\n");
    printf("\n------|Shop Menu|------\n");
    printf("Item prefixes-    cost\n1- Orange         %c%.2f\n2- Apple          %c%.2f\n3- Pear           %c%.2f\n", ch, priceoforange, ch, priceofapple, ch, priceofpear);

    printf("\nHello, how much would you like to spend today?\n%c", ch);
    scanf_s(" %f", &budget);

    printf("\nYour budget for today is %c%.2f\n", ch, budget);
    printf("What would you like to buy please select 1 , 2 or 3 from shop menu\n");

    scanf_s(" %d", &choice);

    if ((choice == 1) && (priceoforange <= budget)){
        printf("Success you have purchased orange for %c%.2f", ch, priceoforange);
        return(0);
    }
    else if((choice == 2) && (priceofapple <= budget)){
        printf("Success you have purchased Apple for %c%.2f", ch, priceofapple);
        return(0);
    }
    else if ((choice == 3) && (priceofpear <= budget)){
        printf("Success you have purchased Pear for %c%.2f", ch, priceofpear);
        return(0);
    }
    else if ((choice < 4) && (priceoforange || priceofapple || priceofpear > budget)) {
        printf("\n*****-|Failure you cant make the purchase you have %c%.2f|-*****\n\n", ch, budget);
        return(0);
    }
    else {
        printf("\n***** -|Failure you have inputted incorrect information|- *****\n\n");
    }
    return(0);
}

您应该检查 scanf_s() 调用返回的值,这将是 成功 读取和分配的项目数。如果小于 1,则没有有效输入 - 如果用户仅输入一个点(单独),就会出现这种情况。

因此,您可以将 prompt/read 放入一个短循环中,就像这样(我还添加了代码以删除任何其他 'incorrect' 输入字符,例如 abc%$-flucnk):

#include <stdio.h>

int main()
{
    float priceoforange = -1.0f;
    int check = 0;
    while (check != 1) {
        printf("\nWhat would you like the cost of orange to be? ");
        check = scanf_s("%f", &priceoforange);
        if (check != 1) {
            printf("Please enter a valid number!\n");
            int c;
            while ((c = getchar()) != '\n' && c != EOF) // Clear other invalid characters!
                ;
        }
    }
    printf("OK - The price of an orange is %f!", priceoforange);
    return 0;
}

如果没有这样的检查,如果 scanf_s 调用失败(与 . 条目一样),那么 没有值 将分配给 priceoforange 变量,它将包含一个未定义的值(除非你之前给它分配了一个 'default' - 你还没有这样做)。