1 Attachment(s)
this is what happens when i enter zero.help me please
this is the code but i don't understand why it breaks immediately when i enter zero it must ask the user for yes or no. i used scanf for it.i need help only for the if(x==0) part(starting with line 104) you dont need to look at the rest of the code thank you
Code:
#include <stdio.h>int main()
{
int x;
char y,n,a;
float z,e,p;
float sum=0;
printf("****************Online Shopping*****************\n");
printf("1- Apple 5$\n");
printf("2- Orange 7$\n");
printf("3- Tomate 9$\n");
printf("4- Meat 50$\n");
printf("5- Chicken 25$\n");
printf("6- Milk 2.5$\n");
printf("7- Yoghurt 4.2$\n");
printf("8- Egg 1.1$\n");
printf("9- Lemonate 1.9$\n");
printf("10-Coke 2.3$\n");
printf("************************************************\n");
while(x!=-1 && x!=0){
scanf("%d",&x);
if(x!=-1 && x!=0){
scanf("%f",&z);
if (x == 1){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Apple: %.2f$\n", z, 5 * z);
sum += 5 * z;}
if (x == 2){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Orange: %.2f$\n", z, 7 * z);
sum += 7 * z;}
if (x == 3){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Tomate: %.2f$\n", z, 9 * z);
sum += 9 * z;}
if (x == 4){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Meat: %.2f$\n", z, 50 * z);
sum += 50 * z;}
if (x == 5){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Chicken: %.2f$\n", z, 25 * z);
sum += 25 * z;}
if (x == 6){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Milk: %.2f$\n", z, 2.5*z);
sum += 2.5*z;}
if (x == 7){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f kg Yoghurt: %.2f$\n", z, 4.2*z);
sum += 4.2*z;}
if (x == 8){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f box Egg: %.2f$\n", z, 1.1*z);
sum += 1.1*z;}
if (x == 9){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): %.2f lt Lemonade: %.2f$\n", z, 1.9*z);
sum += 1.9*z;
}
}
if (x ==-1){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): -1\n");
scanf("%f",&e);
printf("Your total price is %.2f$. How much money you enter: %.2f$\n",sum,e);
if(e>=sum){
printf("Total price has been paid. Your remaining money %.2f$\n",e-sum);
printf("Have a good day...");
break;
}
if(e<sum){
scanf("%f",&p);
printf("Your money is not enough. You must add %.2f$ for total price. How much money you enter: %.2f$\n",sum-e,p);
e+=p;
if(e>=sum){
printf("Total price has been paid. Your remaining money %.2f$.\n",e-sum);
printf("Have a good day...");
break;
}
}
if(x==0){
scanf("%c",&a);
if(a=='y'){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : y\n");
printf("Have a good day");
break;
}
if(a=='n'){
printf("Please enter your choice and quantity(Enter '0' for exit, '-1' for total price): Are you sure (y/n) : n\n");
continue;
}
}
}
}
return 0;
}