Help with sentinel-controlled loop/switch!

A mail order house sells five different products whose retail prices
are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5—
$6.87. Write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a switch statement to determine the retail price for each product. Your
program should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

I get tons of error when I try to compile my code and I was wondering what I did wrong.

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
#include <iostream>
using namespace std;

int main()
{
    int numberOfProducts = 0;
    int costOfProducts = 0;
    int productTotal = 0;
    int amountP1 = 0;
    int amountP2 = 0;
    int amountP3 = 0;
    int amountP4 = 0;
    int amountP5 = 0;
   
    cout<<"How many products do you want to buy? -1 to finish shopping"<<endl;
   
    while(numberOfProducts != -1);
         cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
            switch(costOfProducts);
         {
            case product1:
                cout<<"Product 1($2.98) has been purchased";
                    productTotal = productTotal + 2.98;
                    amountP1 = amountP1 + 1;
                    break;
            case product2:
                cout<<"Product 2($4.50) has been purchased";
                    productTotal = productTotal + 4.50;
                    amountP2 = amountP2 + 1;
                    break;
            case product3:
                cout<<"Product 3($9.98) has been purchased";
                    productTotal = productTotal + 9.98;
                    amountP3 = amountP3 + 1;
                    break;
            case product4:
                cout<<"Product 4($4.49) has been purchased";
                    productTotal = productTotal + 4.49;
                    amountP4 = amountP4 + 1;
                    break;
            case product5:
                cout<<"Product 5($6.87) has been purchased";
                productTotal = productTotal + 6.87;
                amountP5 = amountP5 + 1;
                    break;
					default:
            cout<<"Buy a product";
            numberOfProducts ++;
         }
         
         cout<<"The total amount of products bought are: " << numberOfProducts;
         cout<<"The total amount of product 1's bought is: $" << amountP1 << endl;
         cout<<"The total amount of product 2's bought is: $" << amountP2 << endl;
         cout<<"The total amount of product 3's bought is: $" << amountP3 << endl;
         cout<<"The total amount of product 4's bought is: $" << amountP4 << endl;
         cout<<"The total amount of product 5's bought is: $" << amountP5 << endl;
		 cout<<"The total price of all your products are: $" << productTotal << endl;
         return 0;
}
while(numberOfProducts != -1);

1
2
3
default:
            cout<<"Buy a product";
            numberOfProducts ++;


The user input of the number of products they want to buy isn't stored, so right now the while statement is using the initial value of 0. This is not equal to the sentinel or any of the specific cases, so it will go to the default condition which outputs a message and increments the number of products.


1
2
3
switch(costOfProducts);
         {
            case product1:


What is the user entering for costofProducts? You have that identified as an integer type variable, but your cases are using product1 etc. Are you getting a mismatch error on those?
1)numberOfProducts++; increases the value of numberOfProducts each loop so I'm pretty sure it does stores it.

2)I'm using product 1 2 3 4 5 etc. because I'm asking which products do you want to buy and if they type in "product x" It'll be correct for the case and assignment.

Kinda exhausted at the moment so I might be wrong or something like that.

cout<<"How many products do you want to buy? -1 to finish shopping"<<endl;

Aren't you meaning to ask the user how many products they want to buy? You aren't saving their selection. That's why I'm wondering why you're incrementing the number of products they want to buy from what they've told you.

You're doing the switch on costofProducts which is defined to be an integer variable, but product1 just looks like a variable name. If the person is entering text, that's not going to be stored in an integer variable. (How will they know to enter product x? Are you going to display a menu?)



Edit: syntax for switch - no semi-colon after the switch condition :)

switch(costOfProducts);
should be

switch(costOfProducts)
Last edited on
1) I know I'm not saving it because I'm doing it on the second half of the code. I just did it because at the end, it should be the same as the cin>> of it. I think.

2) Are you suggesting I make int's product 1-5?
I think I would display a menu and ask the user to enter a single number corresponding to a product.

Product Menu
1. Product 1
2. Product 2
3. Product 3
4. Product 4
5. Product 5

Enter your selection (1 - 5):
(or -1 to stop shopping)

I might not even ask how many products they want to buy. Just let them shop until they enter the sentinel.
Last edited on
Hmm I might just try again from scratch. Have to finish this in 1 hour.
Topic archived. No new replies allowed.