#include <stdio.h>
int i, party;
char x = ' ';
float total = 0, perCost = 0;
main(){
switch (toupper(x)) {
case 'A':
printf("Combo A: Friend Chicken with slaw [price: 4.25]");
perCost = 4.24;
break;
case 'B':
printf("Combo B: Roast beef with mashed potato [price: 5.75]");
perCost = 5.75;
break;
case 'C':
printf("Combo A: Fish and chips [price: 5.25]");
perCost = 5.25;
break;
case 'D':
printf("Combo A: soup and salad [price: 3.74]");
perCost = 3.75;
break;
default:
perCost = 0;
break;
}
printf("Enter Party Total: ");
scanf("%d", &party);
for (i = 0; i < party; i++) {
printf("Enter item ordered [A/B/C/D/X]: ");
scanf("%c%*c", &x);
}
total = total + perCost;
printf("%f\n", total);
}
What is causing my programming to not grab from the switch statement?
As per the given code, when the execution first come to the
switch()the value ofxis' ', so the by executing theswitch()givesperCost = 0executing the default condition inswitch()make you believe that the program din grab theswitch().(Note that the execution never come back to here again)To achieve what you supposed, give the
switch()inside thefor (i = 0; i < party; i++)loop, specifically, below yourscanf.Note that
total = total + perCost;is misplaced, as of now, it wont calculate the total but only gives theperCostof the last combo you ordered.This is also supposed to be inside the loop.You need a
#include <cctype.h>in your program.