Thread: C++ Swiches Help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Unhappy 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;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    also i cant get the totals at the end to add up.
    You need to accumulate the running totals for each item.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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;
    }
    Last edited by rogster001; 09-21-2011 at 09:11 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Correction: You may actually want to indent, because otherwise you are making the code very hard for us to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    See Lesson 4: Functions. Also, as has been indicated, you need to learn how to indent your code.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Where is your calculate function? I don't even see you calling it in your code...

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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;
    }

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
    if (QuantitySold() <= 10 )
             Calculate = 1 ;
             
             
          if (QuantitySold() >= 10 && <=20)
             Calculate = 0.10 ;
             
          if (QuantitySold() >20 )
             Calculate = 0.25 ;
    Remove the parenthesis after quantitysold.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Oh and where should i put
    Code:
    setprecision( 2 )

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by 123456tacos View Post
    Oh and where should i put
    Code:
    setprecision( 2 )
    Read: How to format output with cout - FAQ()
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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?

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Don't forget this, you already have the code in place you just need to create a function for it.
    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;
    }

Popular pages Recent additions subscribe to a feed

Tags for this Thread