Arrays

The program below is a menu-driven program that has just 1 choice! Brainstorm! Try adding each of the following functions:

  • Raise all the prices
  • Take an order for several items
  • Check the price of an item
  • What is the cheapest item?
  • What is the most expensive?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Fast food
#include<iostream>
#include<string>
using namespace std;
string food[]={"","hotdog","fries","soda","cookie","pizza","burger",
"taco","shake"};
double price[9]={0,1.75,1.50,0.90,1.25,3.00,3.50,2.00,1.75};
void printAll(); //prints menu
void instructions(); //display choices
void process(char answer); //call correct function
int main() {
  char answer;
  instructions();
  cin>>answer;
  while ((answer != 'q') && (answer != 'Q')) {
    process(answer);
    instructions();
   cin>>answer;
  } //loop until they enter a Q
} //main
void instructions() { //display choices
  cout<<"C++ Fast Food\n";
  cout<<"Enter M to print the menu\n";
  cout<<"Enter Q to quit\n";
  cout<<"Your choice:";
} //instructions
void process(char answer) { //call correct function
  switch (answer) {
    case 'M': case 'm': printAll(); break;
    case 'Q': case 'q': break; //don't go to default
    default: cout<<"Invalid choice\n";
  } //switch
} //process
void printAll() {
  cout<<"Item\tFood\tPrice\n";
  cout.precision(2);
  cout.setf(ios::fixed);
  for (int i=1; i<9; i++)
    cout<<i<<"\t"<<food[i]<<"\t"<<price[i]<<"\n";
}//printAll
CODE
Output:

NEXT: Read a file into an array