0

I have attempted to look for ways to set it up so that it stops a single dot . from being set into a float variable from a scanf_s input but I have turned up nothing so that means a customer can type in just a . dot or full stop and press enter and program runs like that's fine. I want it to fail I attempted to cheat this by doing something like

if(priceoforange == .){
        printf("Failure);
    }

Naturally, I tried every possible way of typing that be it = or == . , '.', ".", . etc and nothing seems to work if anything it wants the . to be a char whereas it's being saved in a variable float and it will let the program do its thing and set the value of the variable that the dot was put into a random like -1000000435.

code is something like this

float priceoforange;

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

scanf_s(" %f", &priceoforange);

this allows for priceoforange to be set to .

#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);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    `scanf_s` is not “storing a .” or setting a value to −10000000. It is not setting the `float` object at all. `scanf_s` returns a result that is the number of items that were assigned or `EOF` if an input failure occurred before any conversion completed. When it sees the dot or other input that does not match the requirements for `%f`, it stops and returns 0. You should check the return value. Use `int result = scanf_s(" %f", &priceoforange);`. Afterward, if `result` is not 1, print a message to the user, scan the input until a newline is seen (to flush the line the user typed), and try again. – Eric Postpischil Oct 20 '20 at 21:51
  • also stop using float for prices. Not only does it suffer from [binary floating point issues](https://stackoverflow.com/q/588004/995714) like $1.1, it can't also store values such as $123456789 because it has only ~7 digits of precision (assuming IEEE-754) – phuclv Oct 21 '20 at 01:57

1 Answers1

2

You should check the value returned by the scanf_s() call, which will be the number of items successfully read and assigned. If this is less than 1, then there was no valid input - as will be the case if the user enters just a single dot (on its own).

So, you could put your prompt/read into a short loop, like this (I have also added code to remove any other 'incorrect' input characters, like 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;
}

Without such checks, if the scanf_s call fails (as with the . entry), then no value will be assigned to the priceoforange variable, and it will contain an undefined value (unless you previously assign it a 'default' - which you haven't done).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/223433/discussion-on-answer-by-adrian-mole-i-want-to-stop-a-float-variable-from-storing). – Samuel Liew Oct 21 '20 at 23:42