### 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include 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"<
`while(numberOfProducts != -1);`

 ``123`` ``````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.

 ``123`` ``````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.

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.