473,379 Members | 1,187 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,379 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 1498
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.