Help with "if" statements

So my professor assigned me this homework yesterday and i'm nearly finished but
I am having a little trouble using the if statements to set limits.
So my professor wants items a,b, and c to be in the range of 1-50 any other number will read a message. And all other orders will be in the range of 1-200 also displaying a message outside those limits.

I havent added the if statements yet because im not too familiar with the syntax.


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

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

/**
 Assume that user can only order one item from the menu. But use can enter a quantity for the item.
 The program displays the items and the item price, the sale tax of 9.25% , and it also asks for a tip amount.
 Then it displays the total sales.
 
 All input numbers can not be negative. Display an error message if they are.
 The maximum order quantity for item a, b, and c is 50.
 The maximum order quantity for the other items is 200.
 Display and error if quantity exceeds the limits.
 
 Sample run:
 
 Welcome to the C++ restaurant. You can choose of the following items :
 
 a- Grilled salmon, price $24.99
 b- New York Steak, price $17.99
 c- Roast Chicken, price $15.99
 d- Salad, price $5.99
 e- Soup, price $7.99
 f- Hamburger, price $4.99
 g- Soft drink, price $1.29
 h- Tea, price $1.50
 i- Orange juice, price $2.50
 
 Enter a letter for the item: a
 Enter the quantity: 3
 
 The item price for (3 x Grilled salmon) is: $74.97
 The sale tax is $6.93
 
 Enter the gratuity amount: 10.00
 
 Your total price is $91.90
 
 Thank you for your order
 *********/

int main()
{
    
    cout  << "Welcome to the C++ restaurant.  You can choose one of the following items: \n" << endl;
    cout  << "a- Grilled salmon, price $24.99\n"
    << "b- New York Steak, price $17.99\n"
    << "c- Roast Chicken, price $15.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" << endl;
    
    char item;
    cout  << "Enter a letter for the item: ";
    cin   >> item;
    
    double price;
    string food;
    
    switch(item)
    {
        case 'a': food = "Grilled Salmon";
            price= 24.99;
            break;
            
        case 'b': food = "New York Steak";
            price= 17.99;
            break;
            
        case 'c': food = "Roast Chicken";
            price= 15.99;
            break;
            
        case 'd': food = "Salad";
            price= 5.99;
            break;
            
        case 'e': food = "soup";
            price= 7.99;
            break;
            
        case 'f': food = "Hamburger";
            price= 4.99;
            break;
            
        case 'g': food = "Soft Drink";
            price= 1.29;
            break;
            
        case 'h': food = "Tea";
            price= 1.50;
            break;
            
        case 'i': food = "Orange Juice";
            price= 2.50;
            break;
            
        default: cout<<"Invalid item!" << endl;
            
    }
    const double tax = 0.0925;
    double grat;
    int quan;
    
    cout  << "Enter the quantity: ";
    cin   >> quan;
   
    cout << "\n";
    cout << setprecision(2) << fixed;
    cout << "The item price for (" << quan << " x " << food << " is: $" << (quan*price) << ")" << endl;
    cout << "The sales tax is: $" << tax*(quan*price) << "\n";
    
    cout << "Enter the gratuity amount: $";
    cin >> grat;
    
    cout << "Your total price is $" << (tax*(quan*price))+(quan*price)+(grat) << "\n" << endl;
    
    cout << "Thank you for your order!" << endl;
    
     return 0;
}
Last edited on
closed account (2LzbRXSz)
if statements are pretty great. Are you confused about just their syntax, or where to put them?

You can read up on if statements here
http://www.cplusplus.com/doc/tutorial/control/

:)
Last edited on
Im kind of new to the if statements but i dont know hwere to put them or arrange them. Heres what Im thinking:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    int quan;
    
    cout  << "Enter the quantity: ";
    cin   >> quan;
   
    if(quan > 0 && quan <= 200)
   {
       if(item == 'a' || item =='b' || item == 'c' && quan >50)
           cout << "Invalid item quantity. It must not exceed 50. " << endl;
       
//the range of items d through i is 1-200
// how do i set it up?
   }
    else
    {
        cout << "Please enter a quantity between 1 and 200." << endl;
    }
       return 0;
Last edited on
closed account (2LzbRXSz)
Since d-i is the last range, you could either do this with an else statement
1
2
3
4
if(item == 'a' || item =='b' || item == 'c' && quan >50)
           cout << "Invalid item quantity. It must not exceed 50. " << endl;
else
          // else 

or, you don't even need the else.
1
2
3
if(item == 'a' || item =='b' || item == 'c' && quan >50)
           cout << "Invalid item quantity. It must not exceed 50. " << endl;
// Continue code here 

Personally though, If you don't use the else, I'd suggest writing your if statement like this
1
2
3
4
5
if(item == 'a' || item =='b' || item == 'c' && quan >50)
{
           cout << "Invalid item quantity. It must not exceed 50. " << endl;
}
// Continue code here 

just so it doesn't look confusing

Edit: I just remembered there's some more things you need to fix so that it doesn't still multiple even after you say the quantity is invalid. Give me a moment to update:)

Edit 2: Move all of this
1
2
3
4
5
6
7
8
9
10
11
cout << "\n";
        cout << setprecision(2) << fixed;
        cout << "The item price for (" << quan << " x " << food << " is: $" << (quan*price) << ")" << endl;
        cout << "The sales tax is: $" << tax*(quan*price) << "\n";
        
        cout << "Enter the gratuity amount: $";
        cin >> grat;
        
        cout << "Your total price is $" << (tax*(quan*price))+(quan*price)+(grat) << "\n" << endl;
        
        cout << "Thank you for your order!" << endl;

into your else statement, otherwise the program continues even after being told the quantity is invalid.
Last edited on
Topic archived. No new replies allowed.