This is probably a simple mistake but Ive honestly no idea why my program is failing

So just trying to create a simple menu as part of another program, It says there are no errors when I build it however when I try execute the code and type in an option It comes up that there's an error.

using namespace std;
#include <iostream>

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
        int itemCode;
	int numItems = 0;
	double price;
	do {
		   cout << "Press 1 for mars bar - 2.50 " << endl
			<< "press 2 for crisps - 1.20 " << endl
			<< "press 3 for coke - 1.00 " << endl
			<< "press 4 for ice cream - 4.10" << endl
			<< "press 5 for pizza - 4.30 " << endl
			<< "press 6 for chocolate  " << endl
			<< "press 0 to exit " << endl;
		cin >> itemNum;

		switch (itemNum)
		{
		case 1:

			price = price + 2.50;
			numItems++;
			break;
		case 2:
			price = price + 1.20;
			numItems++;
			break;
		case 3:
			price = price + 1.00;
			numItems++;
			break;
		case 4:

			price = price + 4.10;
			numItems++;
			break;
		case 5:
			price = price + 4.30;
			numItems++;
			break;
		case 6:
			price = price + 1.30;
			numItems++;
			break;
		default:
			cout << "This is not a vaid option please try again";

		}

	} while (itemNum != 0);

	cout << "Numer of items = " << numItems << endl;
	cout << "Price = " << price << endl;
	
	return 0;
Hi,
This :
while (itemNum != 0);

Should be :
while (itemNum <= 0 || itemNum > 6);
Or (if you want to exit when you enter 0)
while (itemNum < 0 || itemNum > 6);
Apply this code :
1
2
default:
			if(itemNum != 0)cout << "This is not a valid option please try again";
Does that help? :)
closed account (E0p9LyTq)
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
#include <iostream>

int main()
{
   int itemNum;
   int numItems = 0;
   double price = 0.0;

   do
   {
      std::cout << "Press 1 for mars bar - 2.50\n"
         << "press 2 for crisps - 1.20\n"
         << "press 3 for coke - 1.00\n"
         << "press 4 for ice cream - 4.10\n"
         << "press 5 for pizza - 4.30\n"
         << "press 6 for chocolate\n"
         << "press 0 to check out\n: ";
      std::cin >> itemNum;

      switch (itemNum)
      {
      case 1:
         std::cout << "Buying a Mars Bar.\n";
         price += 2.50;
         numItems++;
         break;

      case 2:
         std::cout << "Buying Crisps.\n";
         price += 1.20;
         numItems++;
         break;

      case 3:
         std::cout << "Buying Coke.\n";
         price += 1.00;
         numItems++;
         break;

      case 4:
         std::cout << "Buying Ice Cream.\n";
         price += 4.10;
         numItems++;
         break;

      case 5:
         std::cout << "Buying Pizza.\n";
         price += 4.30;
         numItems++;
         break;

      case 6:
         std::cout << "Buying Chocolate.\n";
         price += 1.30;
         numItems++;
         break;
      
      case 0: // exiting
         break;

      default:
         std::cout << "This is not a valid option please try again...\n";
      }

   } while (itemNum != 0);  // originally had wrong test condition, would exit on neg nums, ooops!

   std::cout << "Number of items = " << numItems << std::endl;
   std::cout << "Price = " << price << std::endl;
}


Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 7
This is not a valid option please try again...
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 1
Buying a Mars Bar.
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 3
Buying Coke.
Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 0
Number of items = 2
Price = 3.5


"closed account", did you bother to actually compile your code before you post? If you did you'd find your "solutions" don't work.

You don't help at all.
Last edited on
closed account (48T7M4Gy)
Same thing but you can condense this quite a lot.
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
#include <iostream>
#include <string>

int main()
{
    std::string product[] = {"dummy", "Mars bar", "Crisps", "Coke", "Icecream", "Pizza", "Chocolate"};
    double cost[]  = {0, 2.50, 1.20, 1.00, 4.10, 4.30, 1.30};
    size_t no_items = sizeof(cost)/sizeof(double);
    int item_no;
    double price = 0;
    int cart = 0;
    
    do{
        
        for(size_t i = 1; i < no_items; i++)
            std::cout << "Press " << i << " for " << product[i] << '\n';
        std::cout << "\nPress 0 to exit\n";
        
        std::cin >> item_no;
        
        switch (item_no)
        {
            case 0:
                std::cout << "You chose to exit.\n";
                break;
                
            case 1 ... 6: // Success with this depends on compiler
                
                price += cost[item_no];
                cart++;
                break;
            
            default:
                std::cout << "This is not a valid option please try again\n";
        }
        
    } while (item_no != 0);
    
    std::cout << "Number of items = " << cart << '\n';
    std::cout << "Price = " << price << '\n';
    
    return 0;
}
Thanks guys @kemort and @Furryguy :)
closed account (E0p9LyTq)
@willmannix, you are welcome.

Please note your and kemort's while test condition is correct while (x != 0). My original one, while(x > 0), didn't work properly for negative numbers.
Why is my solution not working?
while(itemNum < 0 || itemNum > 6);
This loops whatever itemNum is lower than zero or itemNum is greater than 6 (invalid itemNum)

Perhaps itemNum is a std::size_t?

And :
1
2
default:
if(itemNum != 0) cout << "This is not a valid option please try again" << endl;


This code is perfectly legal.
Last edited on
Why is my solution not working?

Because it should loop until the user enters zero.

This code is perfectly legal.

Perhaps but it would be better to have a case statement for all legal entries, including zero. The only time the default statement should execute is when an illegal entry is made.

closed account (E0p9LyTq)
Why is my solution not working?


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

int main()
{
   int itemNum;
   int numItems = 0;
   double price = 0.0;

   do
   {
      std::cout << "Press 1 for mars bar - 2.50\n"
         << "press 2 for crisps - 1.20\n"
         << "press 3 for coke - 1.00\n"
         << "press 4 for ice cream - 4.10\n"
         << "press 5 for pizza - 4.30\n"
         << "press 6 for chocolate\n"
         << "press 0 to check out\n: ";
      std::cin >> itemNum;

      switch (itemNum)
      {
      case 1:
         std::cout << "Buying a Mars Bar.\n";
         price += 2.50;
         numItems++;
         break;

      case 2:
         std::cout << "Buying Crisps.\n";
         price += 1.20;
         numItems++;
         break;

      case 3:
         std::cout << "Buying Coke.\n";
         price += 1.00;
         numItems++;
         break;

      case 4:
         std::cout << "Buying Ice Cream.\n";
         price += 4.10;
         numItems++;
         break;

      case 5:
         std::cout << "Buying Pizza.\n";
         price += 4.30;
         numItems++;
         break;

      case 6:
         std::cout << "Buying Chocolate.\n";
         price += 1.30;
         numItems++;
         break;

      case 0: // exiting
         break;

      default:
         std::cout << "This is not a valid option please try again...\n";
      }

   } while(itemNum < 0 || itemNum > 6);

   std::cout << "Number of items = " << numItems << std::endl;
   std::cout << "Price = " << price << std::endl;
}


Press 1 for mars bar - 2.50
press 2 for crisps - 1.20
press 3 for coke - 1.00
press 4 for ice cream - 4.10
press 5 for pizza - 4.30
press 6 for chocolate
press 0 to check out
: 1
Buying a Mars Bar.
Number of items = 1
Price = 2.5


THAT'S why it doesn't work, a user can only purchase ONE item.

This code is perfectly legal.

Yes it is, but it still is bad code that doesn't work properly.
Topic archived. No new replies allowed.