You should initialize all local variables to zero or some meaningful values.
You assume whole numbers, you should use floats or doubles for all prices and budgets. Are all fruit whole dollars, or less than a dollar a piece?
You only allow the purchase of one apple, etc... at a time, You might want to add number of apples to be purchased at a time.
The user should be able to type 'A', or 'a', for choosing "Apple". Use toupper() to convert 'a', to 'A' for the switch statement.
I converted your scanf_s() to scanf() for my compiling on Linux. scanf_s() is a non-standard Windows function.
There are many ways to do this. Prices of fruit could be read from a text file, etc... I added a function to simplify repeated code.
Based on your code presented, study my version below to start:
Code:
#include <stdio.h>
#include <ctype.h> // For toupper()
int success(int budget, char *item, int price);
int main(void)
{
int Orange = 0;
int Apple = 0;
int Pear = 0;
int Budget = 0;
char ItemChosen = 0;
int RemainingBudget = 0;
printf("Please enter the price of Oranges : ");
scanf("%d", &Orange);
printf("Please enter the price of Apples : ");
scanf("%d", &Apple);
printf("Please enter the price of Pears : ");
scanf("%d", &Pear);
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n");
printf("Please enter your budget : ");
scanf("%d", &Budget);
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", Orange);
printf("A: %d\n", Apple);
printf("P: %d\n", Pear);
printf("**************************************\n");
printf("Please type what item of fruit you would like to purchase : ");
scanf(" %c", &ItemChosen);
switch(toupper(ItemChosen))
{
case 'A' :
RemainingBudget = success(Budget, "Apple", Apple);
break;
case 'P' :
RemainingBudget = success(Budget, "Pear", Pear);
break;
case 'O' :
RemainingBudget = success(Budget, "Orange", Orange);
break;
default:
printf("Your selection is not available\n");
RemainingBudget = Budget;
break;
}
printf("Remaining budget : %d\n", RemainingBudget);
printf("Thanks for shopping with us!\n");
return 0;
}
int success(int budget, char *item, int price)
{
if(price > budget)
{
printf("Error your budget has insufficant funds\n");
}
else
{
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("%s\n", "---------------------------------\n");
printf("Item selected : %s\n", item);
printf("Item price : %d\n", price);
budget -= price;
}
return budget;
}