# C++ Swiches Help

• 09-21-2011
123456tacos
C++ Swiches Help
I need help with with my hw. The code bellow is what i have so far but i still need to this part which i cant figure out .

>>>>>If the quantity sold for the product is over 10, then 10% off is applied to the sales. If the quantity sold for the product is over 20 then 25% off is applied to the sales. You must create a function named Calculate which has 2 parameters to calculate the sales and return the sales. See the function maximum on page 251 for reference.<<<<<<

also i cant get the totals at the end to add up.

This is the complete problem if it helps

Problem Description

A mail-order house sells five different products whose retail prices are as follows: product 1, \$2.98; product 2, \$4.50; product 3, \$9.98; product 4, \$4.49; and product 5, \$6.87. Write an application that reads a series of pairs of numbers as follows:
a) Product number
b) Quantity sold for one day

If the quantity sold for the product is over 10, then 10% off is applied to the sales. If the quantity sold for the product is over 20 then 25% off is applied to the sales. You must create a function named Calculate which has 2 parameters to calculate the sales and return the sales. See the function maximum on page 251 for reference.

Your program must use a switch structure to help determine the retail price for each product. It should calculate and display the total retail value of each product sold last week. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

Code:

```#include<iostream> #include <iomanip.h> using namespace std; int main() { int ProductNumber; double QuantitySold; double sum1=0, sum2=0, sum3=0, sum4=0, sum5=0; cout<<"Enter the product number (1-5) (0 to Stop): "; cin>>ProductNumber; cout<<"Enter quantity sold: "; cin>>QuantitySold; while (ProductNumber != 0 ){ switch (ProductNumber) { case 1: sum1=(2.98*QuantitySold); cout<<"The sales for this Product 1 is \$ "<<sum1<<endl; break; case 2: sum2=(4.50*QuantitySold); cout<<"The sales for this Product 2 is \$ "<<sum2<<endl; break; case 3: sum3=(9.98*QuantitySold); cout<<"The sales for this Product 3 is \$ "<<sum3<<endl; break; case 4: sum4=(4.49*QuantitySold); cout<<"The sales for this Product 4 is \$ "<<sum4<<endl; break; case 5: sum5=(6.87*QuantitySold); cout<<"The sales for this Product 5 is \$ "<<sum5<<endl; break; default: cout << "Invalid product code: " << ProductNumber  << "\n Quantity: " << QuantitySold << '\n';  break;   } cout<<"\nEnter the product number (1-5) (0 to Stop): "; cin>>ProductNumber; cout<<"Enter quantity sold: "; cin>>QuantitySold; } cout<<"Total Sales for the week"<<endl; cout<<"Product 1 \$ "<<sum1<<endl; cout<<"Product 2 \$ "<<sum2<<endl; cout<<"Product 3 \$ "<<sum3<<endl; cout<<"Product 4 \$ "<<sum4<<endl; cout<<"Product 5 \$ "<<sum5<<endl; system ("pause");  return 0; }```
• 09-21-2011
anon
I would assume that the mystery function should accept the quantity and price of one item as arguments and return the price of this quantity with the discounts applied.

Quote:

also i cant get the totals at the end to add up.
You need to accumulate the running totals for each item.
• 09-21-2011
rogster001
You might want to tidy your switch up a bit like this:

Code:

```while (ProductNumber != 0 ) {     float sumVal = 0;     switch (ProductNumber)     {         case 1:         {             sum1=(2.98*QuantitySold);             sumVal = sum1;         }         break;             // more cases..             //..         default:         {             cout << "Invalid product code: " << ProductNumber << "\n Quantity: " << QuantitySold << '\n';         }         break;     }     cout<<"The sales for this Product" << ProductNumber << " is \$ "<< sumVal <<endl; }```
• 09-21-2011
Elysia
Correction: You may actually want to indent, because otherwise you are making the code very hard for us to read.
• 09-21-2011
AndrewHunter
See Lesson 4: Functions. Also, as has been indicated, you need to learn how to indent your code.
• 09-21-2011
rmatze
Where is your calculate function? I don't even see you calling it in your code...
• 09-22-2011
123456tacos
Ok this is my revised code i get a error saying that QuantitySold cant be used as a function and sorry about not spacing my code in the OP

Code:

```#include<iostream> #include <iomanip.h> using namespace std; int main() { int ProductNumber; double QuantitySold; double Calculate; double sum1=0, sum2=0, sum3=0, sum4=0, sum5=0;       cout<<"Enter the product number (1-5) (0 to Stop): ";       cin>>ProductNumber;       cout<<"Enter quantity sold: ";       cin>>QuantitySold;             if (QuantitySold() <= 10 )         Calculate = 1 ;                       if (QuantitySold() >= 10 && <=20)         Calculate = 0.10 ;               if (QuantitySold() >20 )         Calculate = 0.25 ;  while (ProductNumber != 0 ){             switch (ProductNumber) {       case 1:       sum1+=(2.98*QuantitySold*Calculate);       cout<<"The sales for this Product 1 is \$ "<<sum1<<endl;       break;       case 2:       sum2=(4.50*QuantitySold*Calculate);       cout<<"The sales for this Product 2 is \$ "<<sum2<<endl;       break;       case 3:       sum3+=(9.98*QuantitySold*Calculate);       cout<<"The sales for this Product 3 is \$ "<<sum3<<endl;       break;       case 4:       sum4+=(4.49*QuantitySold*Calculate);       cout<<"The sales for this Product 4 is \$ "<<sum4<<endl;       break;       case 5:       sum5+=(6.87*QuantitySold*Calculate);       cout<<"The sales for this Product 5 is \$ "<<sum5<<endl;       break;       default:       cout << "Invalid product code: " << ProductNumber       << "\n Quantity: " << QuantitySold << '\n';       break;   }  cout<<"\nEnter the product number (1-5) (0 to Stop): ";  cin>>ProductNumber;  cout<<"Enter quantity sold: ";  cin>>QuantitySold; }  cout<<"Total Sales for the week"<<endl;  cout<<"Product 1 \$ "<<sum1<<endl;  cout<<"Product 2 \$ "<<+sum2<<endl;  cout<<"Product 3 \$ "<<sum3<<endl;  cout<<"Product 4 \$ "<<sum4<<endl;  cout<<"Product 5 \$ "<<sum5<<endl; system ("pause"); return 0; }```
• 09-22-2011
AndrewHunter
Quote:

Code:

```if (QuantitySold() <= 10 )         Calculate = 1 ;                       if (QuantitySold() >= 10 && <=20)         Calculate = 0.10 ;               if (QuantitySold() >20 )         Calculate = 0.25 ;```

Remove the parenthesis after quantitysold.
• 09-22-2011
123456tacos
Ok i got it working but its giving me the wrong answer For example if i enter product 2 and sold 2 i get \$2.50
• 09-22-2011
123456tacos
Oh and where should i put
Code:

`setprecision( 2 )`
• 09-22-2011
AndrewHunter
Your if statements are screwey. Try this:
Code:

```if(QuantitySold <= 10 )         Calculate = 1 ; else if (QuantitySold > 10 && QuantitySold<=20)         Calculate = 0.10 ; else         Calculate = 0.25 ;```
Also you need to have those statements in your while loop as well.
• 09-22-2011
AndrewHunter
Quote:

Originally Posted by 123456tacos
Oh and where should i put
Code:

`setprecision( 2 )`

Read: How to format output with cout - FAQ()
• 09-22-2011
123456tacos
ok thanks and i got the code part
Code:

`cout << setiosflags( ios::fixed | ios::showpoint )`
im just asking where should i place it and do i have to
Code:

`setprecision( 2 )`
for every result or just one?
• 09-22-2011
AndrewHunter
Either or works. Here is a better reference If you always want it to be a certain precision than you can just do it once as cout.precision(2) for instance.
• 09-22-2011
rmatze
Don't forget this, you already have the code in place you just need to create a function for it.
Quote:

You must create a function named Calculate which has 2 parameters to calculate the sales and return the sales.

replace this: sum1+=(2.98*QuantitySold*Calculate); with your function call and create a function along these lines:

Code:

```double calculate(const double price, int quantity) {     double sum = 0;     sum = price * static_cast<double>(quantity);     if(quantity > 10 && quantity <= 20)         return sum *= .9;     else if(quantity > 20)         return sum *= .75;     return sum; }```