473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

my head hurts

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).
Nov 14 '05 #1
1 1499
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;
}

Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agoston Bejo | last post by:
Hello, I have to write some asp pages that react to HEAD and GET requests. The scenario is this: This is going to be a WAP site. When someone goes to the main page, it is redirected to a payment...
1
by: William Starr Moake | last post by:
This may be difficult to understand unless you are familiar with the design mode feature built into Internet Explorer 5.5+. I used this feature to develop a browser-based WYSIWYG HTML editor for...
6
by: dkintheuk | last post by:
Hi all, I have a form with some unbound combo's and a list box that are used to requery the dataset behind the form. All was working fine until i added the list box. I have the record set...
0
by: Andrew | last post by:
Does a linked list in C require that the head node allocate memory the size of the structures in the node? Or can you just set the head node structure equal to 0? I have tried creating lists both...
3
by: Francois Grieu | last post by:
Given that next is the first field in struct node, and head is a pointer to node, does assigning ((node*)&head)->next safely assign head ? Illustration (this code works on many platforms)...
10
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks ...
7
by: | last post by:
What is the beat way to dynamically write/add to the HEAD tag of an ASPX page (the <head runat=server ... is too error prone and not very repeatable)? Thanks.
3
by: thomasamillergoogle | last post by:
Is it possible that my user control can add code to the HEAD of the parent page that hosts the control?
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
2
by: hon123456 | last post by:
Dear all, I want to know the difference and effect of placing ASP Code in the begining of .asp File and placing code inside <Head</ HeadTag and Placing code inside <body></bodytag. Just as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.