Could Someone help. I need to extend my Restauraunt menu to include functions and a do while loop

Below are the instructions and below that is my code..... Im having trouble adding functions to my switch statements.. Confused on how to pass the value for addOn and make it into a function to add to my switch sorry if my question is confusing In the code below ive added a do while loop and a void function to show the menu but passed that im pretty stuck
Thanks


*******************************
Begin with your corrected code from Program 5B: Restaurant Menu. (Be sure to see my solution and ensure that your code is accurate before you continue with this assignment.)

Extend your restaurant menu program to include:

a do-while loop which redisplays the menu so the user may continue to order additional items and ends when the user chooses to quit
Keep a running total of all items and add ons. That means you need a grand total and an item total variable.
You do NOT need to display an itemized list at the end. Once you learn more C++ skills you will add that feature. For now, simply display the grand total for all items ordered to the user at the end.

Your job is to redesign your code to use functions.

At a minimum, you must:

use functions to validate all user input
create overloaded isValid functions which return a boolean value
use isValid in a while loop or do-while loop to validate user input
use functions for each item menu, so that the switch statement calls a function for each case
similar to the Math Tutor program, display a menu and use a switch statement to call a function
For example, if your Menu consists of:

1. Blueberry Pie

2. Apple Pie

3. Peach Pie

4. Pumpkin Pie

5. Quit

Then create a function called blueberryPie to process the order, adding the cost of blueberry pie to the total and asking the user about 3 options to add on while totaling the price. NOTE: This is mostly a copy and paste of your existing code.

You will need to move constants from main into the function to ensure that the scope (visibility) is accurate OR pass them as parameters. Your choice.

You decide if you use void or value returning functions and if you use value or reference parameters in your functions or none at all.



//*******************************************************************
CODE
*****
// CIS 5 - Online
// Program 5A - Restauraunt Menu
// Date 9/28/2021
#include <iostream>
#include <iomanip>
#include<string>
#include <ctime>
#include <cstdlib>
using namespace std;

void showMenu(); // [MenuFunction][/code]

int main()
{
int choice; // Menu options // Defined Variables
const double itemCost1 = 12.0,
itemCost2 = 17.0,
itemCost3 = 24.0,
itemCost4 = 25.0;
const double addOn = 03.99;
double total;
char answer;
const string item1 = "Spinach Ravioli";
const string item2 = "Shrimp Alfredo";
const string item3 = "Lobster Thermidore";
const string item4 = "Cut Throat Special";

do { // do show menu while choice is not 5
showMenu();
cin >> choice;


switch (choice) // Int Choice switch per number chosen
{
case 1:
total = itemCost1;
cout << "Would you like to add extra seafood?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Would you like extra sauce? ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}

cout << "Would you like to add extra bread?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Your total is, " << total;
break;


case 2:
total = itemCost1;
cout << "Would you like to add extra seafood?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Would you like extra sauce? ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}

cout << "Would you like to add extra bread?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Your total is, " << total;
break;

case 3:
total = itemCost1;
cout << "Would you like to add extra seafood?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Would you like extra sauce? ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}

cout << "Would you like to add extra bread?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Your total is, " << total;
break;

case 4:
total = itemCost1;
cout << "Would you like to add extra seafood?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Would you like extra sauce? ";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
case 5:
cout << " Thanks for choosing Mrs Lovettes :] " << endl
<< "Where you are what you eat!";


cout << "Would you like to add extra bread?";
cin >> answer;
if (answer == 'Y' || answer == 'y')
{
total = total + addOn;
}
cout << "Your total is, " << total;
break;
}
}while (choice != 5);

}
void showMenu() // Show menu function // Do show this menu while choice doesnt = 5 or quit
{
cout << "Mrs Lovettes Meat Pie Emporium" << endl << "Where you are what you eat!\n\n";
cout << "\n\nMenu\n\n";
cout << "1: Spinach Ravioli" << endl;
cout << "2: Shrimp Alfredo" << endl;
cout << "3: Lobster Thermidore" << endl;
cout << "4: Cut Throat Special" << endl << endl;
}






Last edited on
First step would be:
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
Ooh k thanks i usually do. And will remember to keep them in from now on.. any suggestions? i Just figured out how to call by value parameters in functions. Im having confusion figuring out how to use ref parameters in my functions
ie..
doubleFunctionName()
{
function definition
}
Last edited on
If by "using ref parameters" you mean "passing arguments by reference" look here:

https://www.learncpp.com/cpp-tutorial/passing-arguments-by-reference/
i usually do [use code tags] And will remember to keep them in from now on.. any suggestions?

No time like the present to use code tags. You can edit your post and add them now vs. waiting for another post. Even simple code snippets like the one in your 2nd post could benefit from having code tags.
I added code tags after the first time you suggested it. Still no help.. Its cool tho i figured it out hours later on my own. But what counts is i figured it out. Thanks 4n.
It's impossible to read code that isn't formatted. I've formatted it so we can read it, and made two comments on what popped out.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//*******************************************************************
CODE
*****  // kbw: did you forget to comment this line?
// CIS 5 - Online
// Program 5A - Restauraunt Menu
// Date 9/28/2021
#include <iostream>
#include <iomanip>
#include<string>
#include <ctime>
#include <cstdlib>
using namespace std;

void showMenu(); // [MenuFunction]

int main()
{
    int choice; // Menu options // Defined Variables
    const double itemCost1 = 12.0,
                 itemCost2 = 17.0,
                 itemCost3 = 24.0,
                 itemCost4 = 25.0;
    const double addOn = 03.99;
    double total;
    char answer;
    const string item1 = "Spinach Ravioli";
    const string item2 = "Shrimp Alfredo";
    const string item3 = "Lobster Thermidore";
    const string item4 = "Cut Throat Special";

    do { // do show menu while choice is not 5
        showMenu();
        cin >> choice;

        switch (choice) // Int Choice switch per number chosen
        {
        case 1:
            total = itemCost1;
            cout << "Would you like to add extra seafood?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like extra sauce? ";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like to add extra bread?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }
            cout << "Your total is, " << total;
            break;

        case 2:
            total = itemCost1;
            cout << "Would you like to add extra seafood?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like extra sauce? ";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like to add extra bread?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }
            cout << "Your total is, " << total;
            break;

        case 3:
            total = itemCost1;
            cout << "Would you like to add extra seafood?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like extra sauce? ";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like to add extra bread?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
            total = total + addOn;
            }
            cout << "Your total is, " << total;
            break;

        case 4:
            total = itemCost1;
            cout << "Would you like to add extra seafood?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }

            cout << "Would you like extra sauce? ";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }
            // kbw: did we forget the break statement?

        case 5:
            cout << " Thanks for choosing Mrs Lovettes :] " << endl
                << "Where you are what you eat!";


            cout << "Would you like to add extra bread?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                total = total + addOn;
            }
            cout << "Your total is, " << total;
            break;
        }
    } while (choice != 5);
}

void showMenu() // Show menu function // Do show this menu while choice doesnt = 5 or quit
{
    cout << "Mrs Lovettes Meat Pie Emporium" << endl << "Where you are what you eat!\n\n";
    cout << "\n\nMenu\n\n";
    cout << "1: Spinach Ravioli" << endl;
    cout << "2: Shrimp Alfredo" << endl;
    cout << "3: Lobster Thermidore" << endl;
    cout << "4: Cut Throat Special" << endl << endl;
}
Last edited on
Now I can see we, we can look at your query.
Im having trouble adding functions to my switch statements.. Confused on how to pass the value for addOn and make it into a function...


I'm not exactly sure what you want the function to do, but let's say you want a function to and an "add on".
1
2
3
4
void addOn(double& price) {
    const double addOnPrice = 3.99;
    price += addOnPrice;
}


And we can use it like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        case 1:
            total = itemCost1;
            cout << "Would you like to add extra seafood?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                addOn(total);
            }

            cout << "Would you like extra sauce? ";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                addOn(total);
            }

            cout << "Would you like to add extra bread?";
            cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                addOn(total);
            }
            cout << "Your total is, " << total;
            break;


You can also go further, by making each switch case a function:
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
double orderSpinachRavioli()
{
    const double itemCost1 = 12.0;
    double total = itemCost1;
    char answer;

    cout << "Would you like to add extra seafood?";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
        addOn(total);
    }

    cout << "Would you like extra sauce? ";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
        addOn(total);
    }

    cout << "Would you like to add extra bread?";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
        addOn(total);
    }
    return total;
}


If you do that for all your menu items, you main() looks like:
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
int main()
{
    int choice; // Menu options // Defined Variables
    double total = 0.0;

    do { // do show menu while choice is not 5
        showMenu();
        cin >> choice;

        switch (choice) // Int Choice switch per number chosen
        {
        case 1:
            total += orderSpinachRavioili();
            break;

        case 2:
            total += orderShrimpAlfredo();
            break;

        case 3:
            total += orderLobsterThermidor();
            break;

        case 4:
            total += orderShrimpAlfredo();
            break;

        case 5:
            cout << " Thanks for choosing Mrs Lovettes :] " << endl
            break;

        default: // kbw:all other inputs drop here
            ; // kbw: you may add some kind of message
        }
        cout << "Your total is, " << total << endl;
    } while (choice != 5);
}
Last edited on
Topic archived. No new replies allowed.