I'm getting this error when calculating the total bill.
I kinda know what is the issue, it means if an user does not select one of the options, the value of that said "tp" would be undefined because the user did not select any quantity.
Problem is, i do not know how to fix this. How do i make it so that "tp1,2,3,4" will be defined no matter if the user chooses it or not?
Code:
#include <stdio.h>
int main(void) {
int quantity1, quantity2, quantity3, quantity4;
double tp1, tp2, tp3, tp4;
printf("\n\n Product Code Price(USD) \n");
printf(" [1] 20.20 \n");
printf(" [2] 14.50 \n");
printf(" [3] 5.45 \n");
printf(" [4] 2.80 \n");
printf("Enter the product code you wish to purchase : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("You have selected product code 1.");
printf("\nPlease Enter your Quantity :");
scanf("%d", &quantity1);
tp1 = quantity1 * 20.20;
}
break;
case 2:
{
printf("You have selected product code 2.");
printf("\nPlease Enter your Quantity :");
scanf("%d", &quantity2);
tp2 = quantity2 * 14.50;
}
break;
case 3:
{
printf("You have selected product code 3.");
printf("\nPlease Enter your Quantity :");
scanf("%d", &quantity3);
tp3 = quantity3 * 5.45;
}
break;
case 4:
{
printf("You have selected product code 4.");
printf("\nPlease Enter your Quantity :");
scanf("%d", &quantity4);
tp4 = quantity4 * 2.80;
}
break;
default:
{
printf("\nInvalid Choice");
break;
}
}
double totalbill;
totalbill = tp1 + tp2 + tp3 + tp4;
printf("totalbill = %.2f", totalbill);
return 0;
}