help with cin lines in switch statement

closed account (9G8M4G1T)
Can anyone help and explain to me how to correctly include cin lines? My current code does not have any errors but it won't take any user input.

I want to be able to add items with the counter as well as remove but the more I edit my code the more confused I get because I can't see what is being printed out.


Any help is appreciated.


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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
   
const double
    burger_price = 9.00,
    fries_price = 2.75,
    drink_price = 2.5,
    salad_price = 12,
    ice_price = 1.99;
const int
    burger_cal= 500,
    fries_cal= 300,
    drink_cal= 200,
    salad_cal= 400,
    ice_cal= 400;

string totalPrice,
     totalCal;
   
   char choice;
    
    cout << "Place your order from JT's Burger Joint. This app shows your calories and \ncost as you add items to your food order. You can clear, order, or quit at any time.\n";
    cout << "Current order:  burger:0 fries:0 drink:0 salad:0 ice cream:0  TOTAL calories: 0  cost: $0.00\n";
    cout << "Choose: b)urger f)ries d)rink s)alad c)lear, o)order, q)uit: ";
   
    enum menu{burger='b', fries='f', drink='d', salad='s', ice_cream='i', clear='c', order='o', quit='q'};
enum mode_t {Add, Remove};

    menu menuChoice = static_cast<menu>('b'); // this is just to have a value in the menu choice, overridden in the loop

    int b_count=0,
    f_count=0,
    d_count=0,
    s_count=0,
    i_count=0;
    while (menuChoice != quit)
    {
        choice = 'a' ;//user input
    menu menuChoice = static_cast<menu>(choice);

switch(menuChoice){
    case burger:
b_count++;
    break;
    case fries:
f_count++;
    break;
    case drink:
d_count++;
    break;
    case salad:
s_count++;
    break;
    case ice_cream:
i_count++;
    break;
   case clear:
   break;
   case order:
   break;
   case quit:
   break;
        totalPrice = burger_price*b_count + fries_price*f_count + drink_price*d_count + salad_price*s_count+ ice_price*i_count;
        totalCal = burger_cal*b_count + fries_cal*f_count + drink_cal*d_count + salad_cal*s_count+ ice_cal*i_count;
      cin; menuChoice;
      
        cout <<"Current order: burger: " <<b_count<< "fries: "<<f_count<<"drink: "<<d_count<<"salad: "<<s_count<<"ice cream: "<<i_count<< "TOTAL calories: " << totalCal << "cost: $"<< totalPrice << endl ;
    }}
    return 0;
    } 



If possible help me with this part as well: How would I add a - option to remove items, one at a time? and a + option to switch back to adding items. This sets a mode: + mode for adding items; - mode for removing items.

How to not allow negative items, calories, cost. (You cannot remove an item you did not order!)





I'm just starting out and trying to learn please spare me the rude comments.
Last edited on
Your code, lines 67-71 won't be executed. They're not part of any condition in the switch statement.
closed account (9G8M4G1T)
Should I delete 61-66? I think I will need 67-71 for my cout if I'm not mistaken.
Last edited on
I'm not really sure, I'd think you'd want to get the order, then process it. But your code is just in a loop that gets the order.
closed account (9G8M4G1T)
yeah I was told it is in an infinite loop after the cout because the cin lines are not there to wait for user input. I just don't know how to incorporate the necessary cin lines
You want something like this (is pseudocode):
1
2
3
4
5
6
7
8
9
10
11
while (!order_complete) {
    show menu
    selection = get_selection();
    switch (selection) {
        // process the selection--I'll omit the details, except for 'q'
    case 'q':
        order_complete = true;
    }
}

// process order: calculate totals, ... 
As a first revision, perhaps:

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main() {
	constexpr double
		burger_price = 9.00,
		fries_price = 2.75,
		drink_price = 2.5,
		salad_price = 12,
		ice_price = 1.99;

	constexpr int
		burger_cal = 500,
		fries_cal = 300,
		drink_cal = 200,
		salad_cal = 400,
		ice_cal = 400;

	enum menu { burger = 'b', fries = 'f', drink = 'd', salad = 's', ice_cream = 'i', clear = 'c', order = 'o', quit = 'q' };
	enum mode_t { Add, Remove };

	menu menuChoice {};
	int b_count {}, f_count {}, d_count {}, s_count {}, i_count {};

	cout << "Place your order from JT's Burger Joint. This app shows your calories and \ncost as you add items to your food order. You can clear, order, or quit at any time.\n";
	//cout << "Current order:  burger:0 fries:0 drink:0 salad:0 ice cream:0  TOTAL calories: 0  cost: $0.00\n";
	cout << "\nChoose: b)urger f)ries d)rink s)alad c)lear, o)order, q)uit: ";

	while (menuChoice != quit) {
		char choice {};

		std::cout << "\nEnter choice: ";
		std::cin >> choice;
		menuChoice = static_cast<menu>(choice);

		switch (menuChoice) {
			case burger:
				b_count++;
				break;

			case fries:
				f_count++;
				break;

			case drink:
				d_count++;
				break;

			case salad:
				s_count++;
				break;

			case ice_cream:
				i_count++;
				break;

			case clear:
				b_count = d_count = s_count = f_count = i_count = 0;
				break;

			case order:
				break;

			case quit:
				break;

			default:
				std::cout << "Unknown order\n";
				break;
		}

		if (menuChoice != quit) {
			const auto totalPrice = burger_price * b_count + fries_price * f_count + drink_price * d_count + salad_price * s_count + ice_price * i_count;
			const auto totalCal = burger_cal * b_count + fries_cal * f_count + drink_cal * d_count + salad_cal * s_count + ice_cal * i_count;

			cout << "Current order:\nburger: " << b_count << "\nfries: " << f_count << "\ndrink: " << d_count << "\nsalad: " <<
				s_count << "\nice cream: " << i_count << "\nTOTAL calories: " << totalCal << "\ncost: $" << totalPrice << '\n';
		}
	}
}

closed account (9G8M4G1T)
@seeplus thank you it is working properly now :) going to try and figure out how to remove items now
Last edited on
closed account (9G8M4G1T)
Can anyone help me with this:

1
2
3
4
//where should I place this if I want to output this if someone enters the letter o after they have finished ordering

cout << "Thank you for your order! Your total comes to: " << totalPrice << endl <<"Good-bye. Thank you for giving us the opportunity to serve you." <<'\n';

Last edited on
In my code try after L64. You'll also need to modify the condition on L75 to also not display for menuChoice order.
@OP Your data model doesn't lend itself to adding/subtracting or even general processing of orders.
A useful/normal/mundane approach is to treat the job as shopping cart model ie the customer order is collection of items selected.
A collection in this case can be a simple array of items or a vector of items.
Same deal for a price list, once you see the value of collections.
A data collection model will reduce the duplication of effort and increase functionality to include deletions.

If it's simply your original question about cins then have a look at this one and the associated thread http://www.cplusplus.com/forum/beginner/280145/#msg1211029
closed account (9G8M4G1T)
I don't think I will have enough time to add the option to remove items since this is due tonight, but if anyone is willing to help or guide me on where to look I will appreciate that.

The only thing I am having trouble with is the cout if a user inputs the letter o

According to the instructions the output should look like this:
Choose: b)urger f)ries d)rink s)alad i)ce cream c)lear, o)order, q)uit: o

Thank you for your order! Your total comes to: $7.75

Good-bye. Thank you for giving us the opportunity to serve you.


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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int main() {
   //item price
   const double
      burger_price = 9.00,
      fries_price = 2.75,
      drink_price = 2.5,
      salad_price = 12,
      ice_price = 1.99;
   //item calories
   const int
      burger_cal = 500,
      fries_cal = 300,
      drink_cal = 200,
      salad_cal = 400,
      ice_cal = 400;
   //menu options
   enum menu { burger = 'b', fries = 'f', drink = 'd', salad = 's', ice_cream = 'i', clear = 'c', order = 'o', quit = 'q' };
   //counters
   menu menuChoice {};
   int b_count {}, f_count {}, d_count {}, s_count {}, i_count {};
   
   
   cout << "Place your order from J T's Burger Joint. This app shows your calories and cost \nas you add items to your food order. You can clear, order, or quit at any time.\n";
   cout << "Current order: b)urger:" << b_count << " f)ries:" << f_count << " d)rink:" << d_count << " s)alad:" <<
   s_count << " i)ce cream:" << i_count << " TOTAL calories:" << 0 << " cost:$" << 0 << '\n';
   

   while (menuChoice != quit) {
      char choice {};
      
      std::cout << "\nChoose: b)urger f)ries d)rink s)alad c)lear, i)ce cream, c)lear, o)order, q)uit: ";
      std::cin >> choice;
      menuChoice = static_cast<menu>(choice);

      switch (menuChoice) {
         case burger:
            b_count++;
            break;
         case fries:
            f_count++;
            break;
         case drink:
            d_count++;
            break;
         case salad:
            s_count++;
            break;
         case ice_cream:
            i_count++;
            break;
         case clear:
            b_count = d_count = s_count = f_count = i_count = 0;
            break;
         case order:
            cout << "Thank you for your order! Your total comes to: $" << totalPrice << endl <<"Good-bye. Thank you for giving us the opportunity to serve you." <<'\n';
            break;
         case quit:
            std::cout << "Sorry you decided not to order. Please try us again!" << '\n';
            break;
         default:
            std::cout << "Unknown order\n";
            break;
      }
     
      if (menuChoice != quit) {
         if (menuChoice != order) {
            const auto totalPrice = burger_price * b_count + fries_price * f_count + drink_price * d_count + salad_price * s_count + ice_price * i_count;
            const auto totalCal = burger_cal * b_count + fries_cal * f_count + drink_cal * d_count + salad_cal * s_count + ice_cal * i_count;
           
            cout << "Current order: b)urger:" << b_count << " f)ries:" << f_count << " d)rink:" << d_count << " s)alad:" <<
               s_count << " i)ce cream:" << i_count << " TOTAL calories:" << totalCal << " cost:$" << totalPrice << '\n';
            
         
         }

      }
   }
}
Last edited on
Maybe this. To remove an item prefix the letter with a -. so b to order a burger and -b to remove a burger from the order. f for fries, -f to remove fries etc.

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void update(bool del, int& cnt) {
	if (del)
		if (cnt > 0)
			--cnt;
		else
			std::cout << "You don't have any ordered!\n";
	else
		++cnt;
}

int main() {
	//item price
	const double
		burger_price = 9.00,
		fries_price = 2.75,
		drink_price = 2.5,
		salad_price = 12,
		ice_price = 1.99;

	//item calories
	const int
		burger_cal = 500,
		fries_cal = 300,
		drink_cal = 200,
		salad_cal = 400,
		ice_cal = 400;

	//menu options
	enum menu { burger = 'b', fries = 'f', drink = 'd', salad = 's', ice_cream = 'i', clear = 'c', order = 'o', quit = 'q' };

	//counters
	menu menuChoice {};
	int b_count {}, f_count {}, d_count {}, s_count {}, i_count {};

	cout << "Place your order from J T's Burger Joint. This app shows your calories and cost \nas you add items to your food order. You can clear, order, or quit at any time.\n";
	cout << "Current order: b)urger:" << b_count << " f)ries:" << f_count << " d)rink:" << d_count << " s)alad:" <<
		s_count << " i)ce cream:" << i_count << " TOTAL calories:" << 0 << " cost:$" << 0 << '\n';

	while (menuChoice != quit && menuChoice != order) {
		char choice {};
		bool del {};

		std::cout << "\nChoose: b)urger f)ries d)rink s)alad c)lear, i)ce cream, c)lear, o)order, q)uit (- to remove item): ";
		std::cin >> choice;

		if (choice == '-') {
			del = true;
			std::cin >> choice;
		}

		menuChoice = static_cast<menu>(choice);

		switch (menuChoice) {
			case burger:
				update(del, b_count);
				break;

			case fries:
				update(del, f_count);
				break;

			case drink:
				update(del, d_count);
				break;

			case salad:
				update(del, s_count);
				break;

			case ice_cream:
				update(del, i_count);
				break;

			case clear:
				b_count = d_count = s_count = f_count = i_count = 0;
				break;

			case order:
			{
				const auto totalPrice {burger_price * b_count + fries_price * f_count + drink_price * d_count + salad_price * s_count + ice_price * i_count};

				cout << "Thank you for your order! Your total comes to: $" << totalPrice << endl << "Good-bye. Thank you for giving us the opportunity to serve you." << '\n';
			}
				break;

			case quit:
				std::cout << "Sorry you decided not to order. Please try us again!" << '\n';
				break;

			default:
				std::cout << "Unknown order\n";
				break;
		}

		if (menuChoice != quit && menuChoice != order) {
			const auto totalPrice = burger_price * b_count + fries_price * f_count + drink_price * d_count + salad_price * s_count + ice_price * i_count;
			const auto totalCal = burger_cal * b_count + fries_cal * f_count + drink_cal * d_count + salad_cal * s_count + ice_cal * i_count;

			cout << "Current order: b)urger:" << b_count << " f)ries:" << f_count << " d)rink:" << d_count << " s)alad:" <<
				s_count << " i)ce cream:" << i_count << " TOTAL calories:" << totalCal << " cost:$" << totalPrice << '\n';
		}
	}
}

Last edited on
closed account (9G8M4G1T)
@seeplus

Thank you so much! I see what I was missing. Your help is very appreciated
Topic archived. No new replies allowed.