The code is basically asking for the total cost of bags made for each different colour, I wanted the while loop to exit and the total printed and calculated. However I met upon a complication, It works to some extent but I want it to consistently loop until "6" is entered so it can total but it doesn't at all. I was wondering how to get it to loop the main question and exit plus total. It will loop the question, it can exit but the total is equal to a trash figure.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int total, price, bags, colour, cost, Exit ;
printf ("********************************************
Enter 6 to exit if you desire to do so
");
printf ("********************************************
Enter the colour you desire:
1)Black
2)Red
3)Yellow
4)Green
5)Other
********************************************
");
scanf("%d", &colour);
while (Exit!=6)
{
switch (colour)
{
case 1:
{
printf("Enter the number of bags you intend: ");
scanf("%d", &bags);
price=400;
cost=cost+(price*bags);
}
break;
case 2:
{
printf("Enter the number of bags you intend: ");
scanf("%d", &bags);
price=350;
cost=price*bags;
}
break;
case 3:
{
printf("Enter the number of bags you intend: ");
scanf("%d", &bags);
price=120;
cost=cost+(price*bags);
}
break;
case 4:
{
printf("Enter the number of bags you intend: ");
scanf("%d", &bags);
price=200;
cost=cost+(price*bags);
}
break;
case 5:
{
printf("Enter the number of bags you intend: ");
scanf("%d", &bags);
price=50;
cost=cost+(price*bags);
}
break;
}
printf("********************************************
Are you complete with your purchase? If so enter 6
");
scanf("%d", &Exit);
}
printf("The total is %d", total=total+cost);
return 0;
}
question from:
https://stackoverflow.com/questions/65892665/question-on-purchasing-which-involves-a-while-and-switch-statement 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…