C ++ problem using sentinel loop

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

Hi guys, i am a beginner on c++ and i need help on this assignment.

Any help will be appreciate.

#include<iostream>
using namespace std;

int main()

{
float Sales, TotalSales=0, ProductPrice;
int Qty;
int PCode;

cout << "please enter the product number from 1 to 5\n";
cin >> Sales;
cout << "please enter quantity.\n";
cin >> Qty;

while (Sales!=0)
{
cout << "\t\tProduct 1 = $2.98\n";
cout << "\t\tProduct 2 = $4.50\n";
cout << "\t\tProduct 3 = $9.98\n";
cout << "\t\tProduct 4 = $4.49\n";
cout << "\t\tProduct 5 = $6.97\n";

switch (PCode)
{
case '1': PCode=1;
ProductPrice=2.98;
case '2': PCode=2;
ProductPrice=4.50;
case '3': PCode=3;
ProductPrice=9.98;
case '4': PCode=4;
ProductPrice=4.49;
case '5': PCode=5;
ProductPrice=6.87;
case '0': PCode=0;
exit;
default: cout << "Invalid entry, please enter a number from 1 to 5\n";break;
}

Sales= ProductPrice*Qty;

cout << "Your Sales is "<<Sales<<endl;

cout << "Please enter another product number.\n";
cin >> PCode;
cout << "Please enter quantity\n.";
cin >> Qty;

TotalSales=TotalSales + Sales;
}

cout << "Your Total Sales is $ "<< TotalSales <<endl;

return 0;

}

Multiple things

1) When posting code use the code tags ("<>" symbol on the Format pallet) it makes it life easier for us and you

2) float Sales, TotalSales=0, ProductPrice; is bad style
Use
1
2
3
float Sales;
float TotalSales=0;
float ProductPrice;


3) You have declared PCode, not initialized, read as input after the switch statement. Instead of cin >> Sales; did you mean cin >> PCode;

4) Instead of while (Sales!=0) do you mean while (PCode!=0), if so
1
2
case '0': PCode=0;
exit;
is not required

5) The statement after the case labels e.g. case '1': PCode=1; the "PCode = ;" is redundant
Topic archived. No new replies allowed.