hello,i'm the new man in this programming world...and i did not know what wrong with my coding,it won't calculate and i cannot enter how much that i want to pay...i also still not good in doing the subroutine
this the question of what i must do..
write a vending machine program. Beside of allowing the customer to choose the product, your program should also display the amount charged based on the quantity of the product purchased and the balance amount that needs to be return to the customer. (You are required to use subroutines in answering this question)
if (option=='A'||option=='B'||option=='C'||option=='D')
if (option=='A')
while (number!=5)
{
printf("\n\nSelect your food \n\n");
printf("[1]Roti Canai\t\trm1.00\n");
printf("[2]Nasi Lemak\t\trm1.50\n");
printf("[3]Teh Tarik\t\trm1.20\n");
printf("[4]Calculate\n");
printf("[5]Quit Main Menu \n");
fflush(stdin);
printf ("\nSelect your option now by entering respective number\n\n : ");
scanf ("%d", &number);
system("cls");
float total = 0.0;
if (number==1)
{
printf("Roti Canai\t\t rm1.00\n");
printf("Enter the quantity:\n");
scanf("%d",&quantity);
system("cls");
amount= quantity * 1.00;
total = total+amount;
printf("You have to pay :%.2f\n\n", amount);
}
if (number==2)
{
printf("Nasi Lemak\t\trm1.50\n");
printf("Enter the quantity:\n");
scanf("%d",&quantity);
system("cls");
amount= quantity * 1.50;
total=total+amount;
printf("You have to pay :%.2f\n\n", amount);
}
if (number==3)
{
printf("Teh Tarik\t\trm1.20\n");
printf("Enter the quantity:\n");
scanf("%d",&quantity);
system("cls");
amount= quantity * 1.20;
total=total+amount;
printf("You have to pay :%.2f\n\n", amount);
}
if(number==4)
{
total=total+amount;
printf("total=%.2f\n\n", total);
printf("How much you want pay\n");
scanf("%f", &pay);
while (pay >= total)
balance = pay-total;
printf("your balance is %.2f",balance);
First, use [ code ] [ /code ] tags. You can find them on the Format section, highlight your code and press the <> button.
Also, you do realise that this is a C program, not a C++ program? Also, use int main() rather than void main(), avoid using system() calls.
Anyway, in your main function, the option character will never change, so you have an infinite loop there. Also, fflush(stdin) can sometimes produce some wierd problems, so you should probably try to avoid using it (you don't really need it). Other problems we might be able to see if you put your code in "code tags" and format it properly.