I have been writing code for online shopping in C. In the code, if I select that I want to buy all 4
categories and the switch
statement arrives in the Interiors and design Case, Here if I select the no of items I want as 1
the case is repeating itself. This does not happen if I select the no of items as 2
or 3
or 4
it is going to the next case that is is Fashion and design. And the Fashion and design case goes on continuously without terminating. I am not able to figure out the error so can you help me with it?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
void main() {
FILE *fpointer;
char singleLine[1000];
char name[100],ch;
SYSTEMTIME stime;
GetSystemTime(&stime);
int choice, choose, num, *a, n, *b, i, j, m, z, *c, q, *d, t, p, *e;
float quant;
int quant1, quant2, g, quant3;
float total;
float total_e = 0;
float total_g = 0;
float total_f = 0;
float total_i = 0;
float total1 = 0;
float total2 = 0;
float total3 = 0;
float total4 = 0;
float total5 = 0;
float total6 = 0;
float total7 = 0;
float total10 = 0;
float total11 = 0;
float total12 = 0;
float total13 = 0;
float total14 = 0;
float total15 = 0;
float total16 = 0;
float total17 = 0;
float total18 = 0;
float total19 = 0;
float total20 = 0;
float total21 = 0;
float total22 = 0;
float total23 = 0;
float total24 = 0;
float total25 = 0;
char str[1000];
FILE *fp;
fpointer = fopen("groceries1.txt\0", "r");
printf("\n\n\nHistory of purchase is \n");
while (!feof(fpointer)) {
fgets(singleLine, 1000, fpointer);
puts(singleLine);
}
fclose(fpointer);
fp = fopen("groceries1.txt", "a");
a = (int *)malloc(n * sizeof(int));
b = (int *)malloc(n * sizeof(int));
c = (int *)malloc(n * sizeof(int));
d = (int *)malloc(n * sizeof(int));
e = (int *)malloc(n * sizeof(int));
printf("Enter your name \n");
scanf("%s", name);
printf("\nHello %s .We from KK Enterprises welcome you to Online Shopping \n.\n.\n.\n", name);
printf("\nWhat type of product you are looking for \n");
printf(" \n1.Groceries \n 2.Electronic Gadgets \n 3.Interiors/Households \n 4.Fashion \n");
printf("\nEnter the number of categories under which you want to buy \n");
scanf("%d", &n);
printf("\nPlease enter the corresponding key of the category under which your purchase comes\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
switch (a[i]) {
case 1:
printf("Items available :-1.White Rice :- 50 per Kg \n"
" 2.Red Rice :- 40 per kg \n"
"3.Ashirwaad Wheat :- 50 per kg \n"
"4.Surf Exel Powder :- 45 per kg \n"
"5.Bathing Soap :- 50 per qnty \n"
"6. Glass CLeaner :-200 per qnty\n"
"7.Room Freshner :- 400/qnty");
printf("\n \nEnter the number of grocerry items you want \n");
scanf("%d", &m);
printf("\n\n Enter the corresponding key to buy the product \n");
for (j = 0;j < m; j++) {
scanf("%d", &b[j]);
}
for (j = 0;j < m; j++) {
switch (b[j]) {
case 1:
printf("Enter the quantity of White rice you want to add to the cart \n");
scanf("%f", &quant);
total1 = quant * 50;
printf("\n %f is the cost \n", total1);
fprintf(fp, " %f kgs of White rice , Cost Rs%f - %d / %d / %d\n",
quant, total1, stime.wDay, stime.wMonth, stime.wYear);
break;
case 2:
printf("Enter the quantity of Red rice you want to add to the cart \n");
scanf("%f", &quant);
total2 = quant * 40;
printf("\n %f is the cost\n", total2);
fprintf(fp, " %f kgs of Red rice which cost Rs%f - %d / %d / %d\n",
quant, total2, stime.wDay, stime.wMonth, stime.wYear);
break;
case 3:
printf("Enter the quantity of Aashirwaad Wheat you want to add to the cart \n");
scanf("%f", &quant);
total3 = quant * 50;
printf("\n %f is the cost \n", total3);
fprintf(fp, " %f kgs of Aashirwad wheat which cost Rs%f - %d / %d / %d\n",
quant, total3, stime.wDay, stime.wMonth, stime.wYear);
break;
case 4:
printf("Enter the quantity of Surf Exel Powder you want to add to the cart \n");
scanf("%f", &quant);
total4 = quant * 45;
printf("\n %f is the cost\n", total4);
fprintf(fp, " %f kgs of Surf Exel which cost Rs%f - %d / %d / %d\n",
quant, total4, stime.wDay, stime.wMonth, stime.wYear);
break;
case 5:
printf("Enter the number of Bathing Soaps you want add to the cart \n");
scanf("%d", &num);
total5 = num * 50;
printf("\n %f is the cost\n", total5);
fprintf(fp, " %d of Bathing Soap which cost Rs%f - %d / %d / %d\n",
num, total5, stime.wDay, stime.wMonth, stime.wYear);
break;
case 6:
printf("Enter the number of Glass Cleaning Liquid you want add to the cart \n");
scanf("%d", &num);
total6 = num * 200;
printf("\n %f is the cost \n", total6);
fprintf(fp, " %d of Glass Cleaning Liquid which cost Rs%f - %d / %d / %d\n",
num, total6, stime.wDay, stime.wMonth, stime.wYear);
break;
case 7:
printf("Enter the number of Room Freshner Spray you want add to the cart \n");
scanf("%d", &num);
total7 = num * 400;
printf("\n %f is the cost \n", total7);
fprintf(fp, " %d of Room Freshner which cost Rs%f - %d / %d / %d\n",
num, total7, stime.wDay, stime.wMonth, stime.wYear);
break;
default:
printf("Invalid choice \n");
}
}
total_g = total1 + total2 + total3 + total4 + total5 + total6 + total7;
printf(" \n\n Total of groceries is %f \n", total_g);
break;
case 2:
printf("Electronic Gadgets available are \n");
printf("1.One plus 8T Rs 45,000 \n");
printf("2.IPHONE 12 Rs 1,00,000 \n");
printf("3.Samsung Galaxy M20 Rs 20,000\n");
printf("4.Samsung 8K Display 64 inch T.V Rs 4,00,000\n");
printf("5.Sony Bravia 8K Display 64 inch Rs 4,00,000\n");
printf("Enter the number of items you want out of the list above\n");
scanf("%d", &z);
printf("Enter the corresponding key/keys of the products you want\n");
for (t = 0; t < z; t++) {
scanf("%d", &c[t]);
}
for (t = 0; t < z; t++) {
switch (c[t]) {
case 1:
printf("Enter the number of One Plus 8T smart Phones you want\n");
scanf("%d", &quant1);
total10 = quant1 * 45000;
printf("Cost is %f \n", total10);
fprintf(fp, " %d One Plus 8T which cost Rs%f - %d / %d / %d\n",
quant1, total10, stime.wDay, stime.wMonth, stime.wYear);
break;
case 2:
printf("Enter the number of IPHONE 12 smart Phones you want\n");
scanf("%d", &quant1);
total11 = quant1 * 100000;
printf("Cost is %f \n", total11);
fprintf(fp, " %d IPHONE 12 which cost Rs%f - %d / %d / %d\n",
quant1, total11, stime.wDay, stime.wMonth, stime.wYear);
break;
case 3:
printf("Enter the number of Samsung Galaxy M20 smart Phones you want\n");
scanf("%d", &quant1);
total12 = quant1 * 20000;
printf("Cost is %f \n", total12);
fprintf(fp, " %d Samsung Galaxy M20 which cost Rs%f - %d / %d / %d\n",
quant1, total12, stime.wDay, stime.wMonth, stime.wYear);
break;
case 4:
printf("Enter the number of Samsung 8K Display 64 inch T.V you want\n");
scanf("%d", &quant1);
total13 = quant1 * 400000;
printf("Cost is %f \n", total13);
fprintf(fp, " %d Samsung 8K Display 64 inch which cost Rs%f - %d / %d / %d\n",
quant1, total13, stime.wDay, stime.wMonth, stime.wYear);
break;
case 5:
printf("Enter the number of Sony Bravia 8K Display 64 inch T.V you want\n");
scanf("%d", &quant1);
total14 = quant1 * 400000;
printf("Cost is %f \n", total14);
fprintf(fp, " %d Sony Bravia 8K Display 64 inch which cost Rs%f - %d / %d / %d\n",
quant1, total14, stime.wDay, stime.wMonth, stime.wYear);
break;
default:
printf("Invalid Argument \n");
}
}
total_e = total10 + total11 + total12 + total13 + total14;
printf("Total cost of electronics equipments is %f \n", total_e);
break;
case 3:
printf("1.Curtains Rs 4000\n"
" 2.Sofa Set Rs 15,000 \n"
" 3.Bed Rs 20,000 \n"
" 4.Pillows Rs 3,000\n"
" 5.Chairs Rs 15,000\n"
" 6.Tables Rs 10,000\n");
printf("Enter the number of items from the list you want \n");
scanf("%d", &p);
printf("Enter the corresponding key/keys of the items you want to buy \n");
for (i = 0; i < p; i++) {
scanf("%d", &d[i]);
}
for (i = 0; i < p; i++) {
switch (d[i]) {
case 1:
printf("Enter the number of curtain sets you want to buy \n");
scanf("%d", &quant2);
total15 = quant2 * 4000;
printf("Cost is %f \n", total15);
fprintf(fp, " %d Curtain sets which cost Rs%f - %d / %d / %d\n",
quant2, total15, stime.wDay, stime.wMonth, stime.wYear);
break;
case 2:
printf("Enter the number of Sofa sets you want to buy \n");
scanf("%d", &quant2);
total16 = quant2 * 15000;
printf("Cost is %f \n", total16);
fprintf(fp, " %d Sofa set which cost Rs%f - %d / %d / %d\n",
quant2, total16, stime.wDay, stime.wMonth, stime.wYear);
break;
case 3:
printf("Enter the number of Bed sets you want to buy \n");
scanf("%d", &quant2);
total17 = quant2 * 20000;
printf("Cost is %f \n", total17);
fprintf(fp, " %d Bed set which cost Rs%f - %d / %d / %d\n",
quant2, total17, stime.wDay, stime.wMonth, stime.wYear);
break;
case 4:
printf("Enter the number of Pillows you want to buy \n");
scanf("%d", &quant2);
total18 = quant2 * 3000;
printf("Cost is %f \n", total18);
fprintf(fp, " %d Pillows which cost Rs%f - %d / %d / %d\n",
quant2, total18, stime.wDay, stime.wMonth, stime.wYear);
break;
case 5:
printf("Enter the number of chairs you want to buy \n");
scanf("%d", &quant2);
total19 = quant2 * 15000;
printf("Cost is %f \n", total19);
fprintf(fp, " %d Chairs which cost Rs%f - %d / %d / %d\n",
quant2, total19, stime.wDay, stime.wMonth, stime.wYear);
break;
case 6:
printf("Enter the number of tables you want to buy \n");
scanf("%d", &quant2);
total20= quant2 * 10000;
printf("Cost is %f \n", total20);
fprintf(fp, " %d Tables which cost Rs%f - %d / %d / %d\n",
quant2, total20, stime.wDay, stime.wMonth, stime.wYear);
break;
default:
printf("Invalid argument \n");
}
}
total_f = total15 + total16 + total17 + total18 + total19 + total20;
printf("The total cost of Interiors and Households is %f \n", total_f);
break;
case 4:
printf("1.T-Shirt Rs 500 \n"
" 2.Shoes Rs 600 \n"
" 3.Pants Rs 700 \n"
"4.Frocks Rs 800\n"
" 5.Sunglases Rs 200\n");
printf("Enter the no of items u want to purchase from the above list \n");
scanf("%d", &g);
printf("Enter the corresponding key/keys to purchase the item \n");
for (i = 0; i < g; i++) {
scanf("%d", &e[i]);
}
for (i = 0; i < g; i++) {
switch (e[i]) {
case 1:
printf("Enter the number of T-shirts you want to buy \n");
scanf("%d", &quant3);
total21 = quant3 * 500;
printf("Cost is %f\n", total21);
fprintf(fp, " %d T-shirts which cost Rs%f - %d / %d / %d\n",
quant3, total21, stime.wDay, stime.wMonth, stime.wYear);
break;
case 2:
printf("Enter the number of Shoes you want to buy \n");
scanf("%d", &quant3);
total22 = quant3 * 600;
printf("Cost is %f\n", total22);
fprintf(fp, " %d Shoes which cost Rs%f - %d / %d / %d\n",
quant3, total22, stime.wDay, stime.wMonth, stime.wYear);
break;
case 3:
printf("Enter the number of Pants you want to buy \n");
scanf("%d", &quant3);
total23 = quant3 * 700;
printf("Cost is %f\n", total23);
fprintf(fp, " %d Pants which cost Rs%f - %d / %d / %d\n",
quant3, total23, stime.wDay, stime.wMonth, stime.wYear);
break;
case 4:
printf("Enter the number of Frocks you want to buy \n");
scanf("%d", &quant3);
total24 = quant3 * 800;
printf("Cost is %f\n", total24);
fprintf(fp, " %d Frocks which cost Rs%f - %d / %d / %d\n",
quant3, total24, stime.wDay, stime.wMonth, stime.wYear);
break;
case 5:
printf("Enter the number of Sunglases you want to buy \n");
scanf("%d", &quant3);
total25 = quant3 * 200;
printf("Cost is %f\n", total25);
fprintf(fp, " %d Sunglases which cost Rs%f -2 %d / %d / %d\n",
quant3, total25, stime.wDay, stime.wMonth, stime.wYear);
break;
default:
printf("Invalid argument\n");
}
}
total_i = total21 + total22 + total23 + total24 + total25;
printf("Total cost of Fashion/design items are %f", total_i);
break;
default:
printf("Invalid choice\n");
}
}
total = total_g + total_f + total_e + total_i;
printf("Your Bill is %f \n", total);
fclose(fp);
}