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