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.
#include <iostream>
usingnamespace 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;
}
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 :)
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.