Arrays

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
int sandwitch_type;

        cout << "\tTypes of Sandwitch Available:\n"  << endl;
	cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<endl;
	cout << "\t               |\tPlain\tRoll"<< endl;
	cout << "\t1 Cheese       |\t$2.50\t3.00"<< endl;
	cout << "\t2 Ham          |\t$3.00\t3.50"<< endl;
        cout << "\t3 Corned Beef  |\t$3.00\t3.50"<< endl;
        cout << "\t4 Chicken      |\t$3.20\t3.70"<< endl;
        cout << "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<endl;
cout << "What Type of Sandwitch Would You Like to Purchase? "<<endl;	//Ask type of sandwitches
cin >> sandwitch_type;


how can i use arrays for this type of question
something like
 
sandwitch_type[4]

arrays for the types of sandwiches
please help i dont know hot to go about it
Hi,

you could use a struct or, better, a class where you store your info. So you create a class named Sandwich which has some fields like "name" (a string), "price_plain" (a float) and "price_roll" (another_float). Then you build an "archive" of your sandwiches, where you hard-code (or maybe read from a file) all the info about the sandwiches and their price. Finally you write a simple method which prints the information stored in this "archive", exactly as you did before.

P.S. The "archive" I am talking about could be just an array of your object Sandwich.
Last edited on
The simplest solution to use would be a struct.
http://www.cplusplus.com/doc/tutorial/structures/

(@kunz - I get the impression that you haven't covered classes fully yet??)

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct product {
  int weight;
  double price;
};
// "borrowed" from tutorial on structs

const int productCount = 4;
// better to use a const than a literal as can then change the size of
// the array be altering one const rather than changing assorted loops,
// if conditions and the like.

product myProducts[productCount] = {0};
// the = {0} zeros all array elements

myProducts[0].weight = 4;
myProducts[0].price = 1.25;
myProducts[1].weight = 4;
myProducts[1].price = 1.25;
// etc. 


But as C++ programmer you should be working towards a nicely encapsulated, class-based solutions, as outlined by minomic:
http://www.cplusplus.com/doc/tutorial/classes/

But I wouldn't necessarily use a "Sandwich" class. Think generalization/specialization: What happens when the cafe starts selling other products? Does a sandwich behave differently than other products? Etc.

And it looks like all the Roll versions costs 50c more than the corresponding plain version, so it might be better to handle rolls as an extra rather than giving each sandwich type two prices (What happens when you start to sell paninis and thins, too? Four prices for each sandwich?)

Aside:

double is the default floating-point type to use these days. I'd only expect to see float used when you're trying to save memory (which means LOTS of value!) and you know the reduced precision won't cause any issues.

Yes, the price of a sandwich could be handled satisfactorily by a float, but it would be better to get into the habit of using the right type from the off!

(You could even use an int for the prices if you stored them in cents/cents/pence/... rather than dollar/euros/pounds/... In some cases it's better to deal with moneyin this way, as it only usually has 2dp.)

Andy

PS When do you use float and when do you use double
http://programmers.stackexchange.com/questions/188721/when-do-you-use-float-and-when-do-you-use-double
n summary, float and long double should be reserved for use by the specialists, with double for "every-day" use.

Last edited on
Topic archived. No new replies allowed.