#include <stdio.h>
static inline int readInteger()
{
int item = -1;
for(int i = 0; i < 100; ++i)
{
int ch = fgetc(stdin);
if (ch >= '0' && ch <= '9')
{
item = ch - '0';
break;
}
else if (ch <= ' ')
{
continue;
}
else if (ch >= '!')
{
item = 0;
break;
}
}
return item;
}
static inline void displayLine(const char* s)
{
if (s != NULL)
printf("%s\n", s);
}
static inline void displayTotal(const double totalCost)
{
printf("Your item/items total is: $%0.2lf\n", totalCost);
}
static inline void displayStoreMenu()
{
displayLine("1 - Soda: $1.00");
displayLine("2 - Bagel: $1.48");
displayLine("3 - Coffee: $2.15");
displayLine("4 - Scone: $1.89");
displayLine("5 - Orange Juice: $1.75");
displayLine("6 - Muffin: $0.75");
displayLine("0 - Quit");
displayLine("Enter the item number that you want: ");
}
static const double ITEM_COST[8] = {
0.00d,
1.00d,
1.48d,
2.15d,
1.89d,
1.75d,
0.75d,
0.00d
};
int main(int argc, char **argv)
{
short done = 0;
double totalCost = 0.0d;
displayStoreMenu();
while (!done)
{
const int item = readInteger();
switch(item)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
totalCost += ITEM_COST[item];
break;
case 0:
displayTotal(totalCost);
done = 1;
break;
default:
displayLine("Invalid Selection");
break;
}
}
return 0;
}