On Tue, 20 Jul 2004 21:17:58 -0700, Jeffrey Barrett wrote:
First, obviously this is for a class I'm in. Second, I should have
attended the last lecture. Third, I have a headache.
The assignment is to build a soda machine program that will ask the
user to pick a drink (6 choices) and then ask the user for money
(price) and then display the quantity left of the certain drink
(intitially 20).
The assignment states that I must use a) 2 parallel one-dimensional
arrays to store drink prices in the first, and quantity in the second;
b) enumerated type to represent the drink names and values of this
type to access the array elemements for each drink.)
Rather than show what I have done so far (trust me it's laughable),
could someone please just get me started in the right direction (i.e.
quick summary what exactly 'typedef enum' does and how to set up the 2
arrays in parallel).
I will put the person who helps me in my will (I'm rich I swear).
Hopefully you got something sensible from
the past answers,and got your soda maching working.
Would be better done as a state machine, but this soda machine
(made by Acme El'cheapo) might comply.
#include <stdio.h>
#include <stdlib.h>
enum Drink {
Water,
Coke,
Pepsi,
CokeLight,
DrPepper,
IceTea
};
typedef enum Drink Drink;
static int price[6] = { 100, 110, 110, 90, 100, 122 };
static unsigned int quantity[6] = { 20, 20, 20, 20, 20, 20 };
static int ask_for_drink()
{
int choice;
int ret;
choice = 0;
puts("Please pick a drink");
printf("1. Water(%u left)\n", quantity[Water]);
printf("2. Coca Cola(%u left)\n", quantity[Coke]);
printf("3. Pepsi(%u left)\n", quantity[Pepsi]);
printf("4. Coke Light(%u left)\n", quantity[CokeLight]);
printf("5. DrPepper(%u left)\n", quantity[DrPepper]);
printf("6. Ice Tea(%u left)\n", quantity[IceTea]);
do {
printf("Drink Nr: ");
ret = scanf("%d", &choice);
} while (ret != 1 && getchar());
return choice;
}
static void give_cents_back(int cents)
{
int i;
printf("%d cents back\n", cents);
for (i = 1; i <= cents; i++) {
putchar('.');
if (i % 80 == 0) {
putchar('\n');
}
}
putchar('\n');
}
static int get_money(Drink choosen)
{
int money_in;
int ret;
money_in = 0;
printf("This costs %u cents\n", price[choosen]);
printf("Insert cents now, or -1 to abort: ");
while (1) {
int in_tmp;
int diff;
ret = scanf("%d", &in_tmp);
if (ret != 1) {
puts("Illegal coin !?");
getchar();
continue;
} else if (in_tmp == -1) {
return 0;
} else if (in_tmp < 0) {
puts("Er, this is not a minibank");
continue;
}
money_in += in_tmp;
diff = money_in - price[choosen];
if (diff == 0) {
puts("Accepted. No money back\n");
return 1;
} else if (diff > 0) {
printf("Accepted.");
give_cents_back(diff);
return 1;
}
printf("Need another %d cents ", abs(diff));
}
}
Drink numeric_to_drink(int num)
{ /*well, we could just cast it 8) */
switch (num) {
case 1:
return Water;
case 2:
return Coke;
case 3:
return Pepsi;
case 4:
return CokeLight;
case 5:
return DrPepper;
case 6:
return IceTea;
default:
printf("Faulty Soda Machine, Powering Off");
abort();
}
}
static int check_quantity(Drink d)
{
return quantity[d] > 0;
}
static void hand_out_drink(Drink choosen)
{
quantity[choosen]--;
puts("Kalunk..klurr...klank");
puts("Enjoy!");
}
int main(int argc, char *argv[])
{
while (1) {
int choice;
int money_ok;
Drink d;
choice = ask_for_drink();
if (choice < 1 || choice > 6) {
printf("'%d' is unknown\n'", choice);
continue;
}
d = numeric_to_drink(choice);
if (!check_quantity(d)) {
puts("Sorry, nothing left\nMake another choice ");
continue;
}
money_ok = get_money(d);
if (!money_ok) {
continue;
}
hand_out_drink(d);
}
return 1;
}