Need help from the mighty anon (2)

1 Name: #!/usr/bin/anonymous : 2008-09-17 02:46 ID:PaiA0Xdu

Ok, so, I'm kinda stuck here. I'm a newbie at C++ programming, and is a subject I'm seeing at college. After too much object oriented Java, I'm now having some problems at structural programming. Could you please either link me to or post some code for a cashier? That is, the user needs to input the product's name, the program compares it with some pre-existant ones and then emits price, and based on another user input (what he is paying) output the remaining money. Thank you so much beforehand for any kind of help you can provide.

2 Name: #!/usr/bin/anonymous : 2008-09-17 11:00 ID:Heaven

Easy peasy.

First, products:

struct { char *name; double price; } products[] = {
{ "product A", 10,25 },
{ "product B", 20.15 }
};

Then, the interface

double getprice(const char *name);

Which simply iterates the products array, and compares name to products[i].name.
If they are equal, it returns products[i].price.
If a product with such name is not found, you can return NaN.

then the algorithm is trivial:

while(get_input)
    if((d = getprice(input)) != NaN) {
get_money
if(money < d) print "not enough money"
else print "Change: " d - money
}

If you can't write the program after so many hints, you shouldn't be in college.

This thread has been closed. You cannot post in this thread any longer.