Is it possible to set a range for input value?

Im extremely new to C++. I wrote this code that displays the menu for a restaurant, with the prices as well. The program will take in the quantity, the price, sales tax, and the tip you'd like to provide and calculate the total bill for you. Im looking for a way to limit the range for the items on the menu. some way to prevent anyone from inputing greater than a quantity value of 25 for the items. Also, if anyone has any suggestions about how to simplify the source code I would appreciate it as well. thanks
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int choice;
    int quantity;
    double price;
    double salesTax;
    double tip;
    double totalBill;

    const double SALMON = 26.99,
                 STEAK = 18.99,
                 CHICKEN = 12.99,
                 SALAD = 5.99,
                 SOUP = 7.99,
                 HAMBURGER = 4.99,
                 SODA = 1.29,
                 TEA = 1.50,
                 OJ = 2.50;

    const int SALMON_CHOICE = 1,
              STEAK_CHOICE = 2,
              CHICKEN_CHOICE = 3,
              SALAD_CHOICE = 4,
              SOUP_CHOICE = 5,
              HAMBURGER_CHOICE = 6,
              SODA_CHOICE = 7,
              TEA_CHOICE = 8,
              OJ_CHOICE = 9;

    cout << "" << "\n"
         << "\t\tWelcome to the Java restaurant.\n            You can choose from the following items: \n\n"
         << "1. Grilled Salmon, price $26.99\n"
         << "2. New York Steak, price $18.99\n"
         << "3. Roast Chicken, price $12.99\n"
         << "4. Salad, price $5.99\n"
         << "5. Soup, price $7.99\n"
         << "6. Hamburger, price $4.99\n"
         << "7. Soft drink, price $1.29\n"
         << "8. Tea, price $1.50\n"
         << "9. Orange Juice, price $2.50\n\n"
         << "Select a number from the menu: ";
    cin >> choice;

    cout << fixed << showpoint << setprecision(2);

    if (choice == SALMON_CHOICE)
    {
        cout << "Enter the quantity: ";
        cin >> quantity;
        price = quantity * SALMON;
        salesTax = .0925 * price;
        cout << "\n"
             << "The item price for ("<< quantity << " x Grilled Salmon) is $" << price << "\n"
             << "The sales tax is $" << salesTax << "\n"
             << "\n"
             << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == STEAK_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * STEAK;
       salesTax = .0925 * price;
        cout << "\n"
             << "The item price for ("<< quantity << " x New York Steak) is $" << price << "\n"
             << "The sales tax is $" << salesTax << "\n"
             << "\n"
             << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == CHICKEN_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * CHICKEN;
       salesTax = .0925 * price;
       cout  << "\n"
             << "The item price for ("<< quantity << " x Roasted Chicken) is $" << price << "\n"
             << "The sales tax is $" << salesTax << "\n"
             << "\n"
             << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == SALAD_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * SALAD;
       salesTax = .0925 * price;
       cout  << "\n"
             << "The item price for ("<< quantity << " x Salad) is $" << price << "\n"
             << "The sales tax is $" << salesTax << "\n"
             << "\n"
             << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == SOUP_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * SOUP;
       salesTax = .0925 * price;
       cout << "\n"
            << "The item price for ("<< quantity << " x Soup) is $" << price << "\n"
            << "The sales tax is $" << salesTax << "\n"
            << "\n"
            << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == HAMBURGER_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * HAMBURGER;
       salesTax = .0925 * price;
       cout << "\n"
            << "The item price for ("<< quantity << " x Hamburger) is $" << price << "\n"
            << "The sales tax is $" << salesTax << "\n"
            << "\n"
            << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == SODA_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * SODA;
       salesTax = .0925 * price;
       cout << "\n"
            << "The item price for ("<< quantity << " x Soft drink) is $" << price << "\n"
            << "The sales tax is $" << salesTax << "\n"
            << "\n"
            << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == TEA_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * TEA;
       salesTax = .0925 * price;
       cout << "\n"
            << "The item price for ("<< quantity << " x Tea) is $" << price << "\n"
            << "The sales tax is $" << salesTax << "\n"
            << "\n"
            << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
        cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    else if (choice == OJ_CHOICE)
    {
       cout << "Enter the quantity: ";
       cin >> quantity;
       price = quantity * OJ;
       salesTax = .0925 * price;
       cout << "\n"
            << "The item price for ("<< quantity << " x Orange Juice) is $" << price << "\n"
            << "The sales tax is $" << salesTax << "\n"
            << "\n"
            << "Enter the gratuity amount: ";
        cin >> tip;
        totalBill = (price + salesTax + tip);
       cout << "\n"
             << "Your total bill is $" << totalBill << "\n"
             << "\n"
             << "Thank you for your order"<<endl;
    }
    return 0;
}
Last edited on
Hi,

Firstly to streamline your code quite a bit, each else if statement is almost the same - you could make a function which takes arguments for the quantity, the unit price, string description. Also make a separate function to do the gratuity. Then have each else if call the function for the particular product, and call the gratuity function once at the end. The menu should be a function as well. Also always include an else statement to catch bad input.

To limit or and / or validate some input write another function with an if statement:

1
2
3
if (quantity < 0 || quantity > 25) {
   std::cout << "Invalid Quantity amount\n";
} 


Btw, the quantity and choice variables should be unsigned int - we don't ever want them to be negative. Using the type system helps with writing good code.
Last edited on
closed account (48T7M4Gy)
Where you have repetitive code you might consider functions. Instead of complex if's a switch control might be better. And as for your data, consider arrays or other containers. With your menu the selection driving your switch control or the ifs can be enclosed in a while loop.

There are a multitude of examples in old forums and the tutorials will help.

if (quantity > 25)
quantity = 0 i.e. reset
message " You can't have more than 25 please reorder that item"
Last edited on
Thanks for your help. I was able to limit the quantity, and I used unsigned int instead if just int, that makes a lot more sense especially when I can't have any negative values. Now I have to figure out how stop using the code over and over again for each else if statement.
Have you learned array?
No not yet. should I?
> No not yet. should I?
You really should. If you use array, your current code will become 300% more efficient and cleaner.
closed account (48T7M4Gy)
Check out the tutorials here on functions and arrays both of which are fundamental building blocks in this language.

http://www.cplusplus.com/doc/tutorial/

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/
I have updated it a little bit, and everything works how it is supposed to. except now when an error occurs from a user that uses the program, it will notify them of the error, but it doesn't stop the program from running. It will give error message and then ask for a tip and all that.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char choice;
unsigned int quantity; // unsigned int so that any negative quantities will come back as an error //
double price;
double salesTax;
double tip;
double totalBill;

const double salmon = 26.99,
steak = 18.99,
chicken = 12.99,
salad = 5.99,
soup = 7.99,
hamburger = 4.99,
soda = 1.29,
tea = 1.50,
oj = 2.50;

const char salmonChoice = 97,
steakChoice = 98,
chickenChoice = 99,
saladChoice = 100,
soupChoice = 101,
hamburgerChoice = 102,
sodaChoice = 103,
teaChoice = 104,
ojChoice = 105;

cout << "" << "\n"
<< "\t\tWelcome to the Java restaurant.\n You can choose from the following items: \n\n"
<< "a. Grilled Salmon, price $26.99\n"
<< "b. New York Steak, price $18.99\n"
<< "c. Roast Chicken, price $12.99\n"
<< "d. Salad, price $5.99\n"
<< "e. Soup, price $7.99\n"
<< "f. Hamburger, price $4.99\n"
<< "g. Soft drink, price $1.29\n"
<< "h. Tea, price $1.50\n"
<< "i. Orange Juice, price $2.50\n\n"
<< "Select a letter from the menu: ";
cin >> choice;

if (choice < 97 || choice > 105) // error message if choice selection is out of range
{
cout << "Invalid selection please make a valid selection.\n";
choice = 0;
}

cout << "Enter the quantity: "; // input for how many of wanted items you would like
cin >> quantity;

cout << fixed << showpoint << setprecision(2); //sets the amount of spaces to 4 for money amount

if (choice == salmonChoice) // if user chooses grilled salmon
{
if (quantity > 50) //sets max quantity for items, same for all items
{
cout << "Invalid item quantity. It must not exceed 50 or be less than 0.\n";
quantity = 0;
}
price = quantity * salmon;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Grilled Salmon) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == steakChoice) // if user chooses steak
{
if (quantity > 50)
{
cout << "Invalid item quantity. It must not exceed 50 or be less than 0.\n";
quantity = 0;
}
price = quantity * steak;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x New York Steak) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == chickenChoice) //if user chooses chicken
{
if (quantity > 50)
{
cout << "Invalid item quantity. It must not exceed 50 or be less than 0.\n";
quantity = 0;
}
price = quantity * chicken;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Roast Chicken) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == saladChoice) // if user chooses salad
{
if (quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * salad;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Salad) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";

}

else if (choice == soupChoice) // if user chooses soup
{
if (quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * soup;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Soup) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == hamburgerChoice) // if user chooses hamburger
{
if (quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * hamburger;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Hamburger) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == sodaChoice) // if user chooses soft drink
{
if (quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * soda;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for (" << quantity << " x Soft Drink)" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == teaChoice) // if user chooses tea
{
if (quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * tea;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Tea) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

else if (choice == ojChoice) // if user chooses orange juice
{
if ( quantity > 200)
{
cout << "Invalid item quantity. It must not exceed 200 or be less than 0.\n";
quantity = 0;
}
price = quantity * oj;
salesTax = .0925 * price;
cout << "\n"
<< "The item price for ("<< quantity << " x Orange Juice) is $" << price << "\n"
<< "The sales tax is $" << salesTax << "\n";
}

cout << "\n" /* this is the code for the tip*/
<< "Enter the gratuity amount: $";
cin >> tip;

totalBill = (price + salesTax + tip); /* this is the code for the total bill price*/
cout << "\n"
<< "Your total bill is $" << totalBill << "\n"
<< "\n"
<< "Thank you for your order\n"
<< "\n";

return 0;
}
closed account (48T7M4Gy)
Well done, there's nothing worse than Incorrect user input causing a program to just crash. Error trapping, recovery and reporting is part of the programming challenge.
Topic archived. No new replies allowed.