Need help finishing with this c++ project
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Need help finishing with this c++ project

  1. #1
    Join Date
    Jun 2017
    Posts
    3

    Need help finishing with this c++ project

    I need help on how to get my program like it is.
    prompt:
    One large chemical company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9 percent of $5000, or a total of $650. Develop a C++ program that uses a while statement to input each salesperson’s gross sales for last week and calculates and displays that salesperson’s earnings. Process one salesperson’s figures at a time.
    Your program should have one member data named grossSales.
    Your program should have a constructor, a get and a set member function for the data.
    The result salaries must be displayed in 2 decimal places.

    Sample run:
    Enter sales in dollars-1 to end): 5000 //input
    Salary is : 650.00 //output
    Enter sales in dollars-1 to end): 5678
    Salary is: 711.02

    cpp:
    Code:
    #include <iostream>
    #include "Sales.h"
    using namespace std;
    
    int main()
    {
    	double salary, sales;
        Sales mySales;
        //double commision=0.09;//rate given
        
        
    	cout<<"Enter sales In dollar(-1 to end)";
        cin>>sales; //input
    
    // write the loop to get more values
    while(sales!=-1)
    {
    	
    }
            mySales.setGrossSales( sales );
            salary = mySales.findSalary();
    
            cout<<"Salary is "<<salary<<endl;//outputs the salary
    
    }
    .h:
    Code:
    class Sales
    {
      public:
    
       // write your constructor here
    
      int getmember()
      {
      	   return grosssales;
      }
    
      void setGrossSales( double sales) 
      {
    	while(sales!=-1)
    	{
    		grosssales=sales;
    	}
      	//  write codes here
      }
      
      double findSalary()
      {
      	
      	 salary=(0.09*grosssales)+200; 
      	 return salary;
      	  // calculate and return the salary using grosssales
      }
    
      private:
      	double grosssales;
    };
    Last edited by 2kaud; June 16th, 2017 at 01:49 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,944

    Re: Need help finishing with this c++ project

    Quote Originally Posted by jlin55 View Post
    ...
    .h:
    Code:
    class Sales
    {
      public:
    
    ...
      void setGrossSales( double sales) 
      {
    	while(sales!=-1)
    	{
    		grosssales=sales;
    	}
      	//  write codes here
      }
       ...
      private:
      	double grosssales;
    };
    And what this code for setGrossSales should mean? Endless loop for any parameter not equal /1?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,650

    Re: Need help finishing with this c++ project

    a C++ program that uses a while statement to input each salespersonís gross sales for last week and calculates and displays that salespersonís earnings. Process one salespersonís figures at a time.
    Your program should have one member data named grossSales.The result salaries must be displayed in 2 decimal places.
    Well for the simplest program that accomplishes this, consider (without error detection)
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	double grossSales = 0.0;
    
    	while ((cout << "Enter sales In dollar (-1 to end): ") && (cin >> grossSales) && (grossSales > 0))
    		cout << "Salary is: " << fixed << setprecision(2) << showpoint << 0.09 * grossSales + 200 << endl;
    }
    but as the spec for this program assignment requires use of a constructor and get/set member functions, I guess you are learning how to use classes?

    Within the class, for setGrossSales this is just the reverse of getmember() (which really should be renamed something like getGrossSales).
    without the while loop. As each salespersonís figures are to processed one at at a time, the while loop just goes in main(). Then within the loop you ask for the sales figure to be entered and then calculate and display the salary. So within main()
    Code:
    	cout<<"Enter sales In dollar(-1 to end)";
        cin>>sales; //input
    and
    Code:
     mySales.setGrossSales( sales );
            salary = mySales.findSalary();
    
            cout<<"Salary is "<<salary<<endl;//outputs the salary
    goes within the while loop.
    Last edited by 2kaud; June 16th, 2017 at 04:29 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.2)

  4. #4
    Join Date
    Jun 2017
    Posts
    3

    Re: Need help finishing with this c++ project

    setGrossSales() is the setter method

  5. #5
    Join Date
    Jun 2017
    Posts
    3

    Re: Need help finishing with this c++ project

    Here is what I have so far:
    I was able to get the user input the sales, but it does not show the salary right after, but if the user would then input the next it does show the salaries. Any way you can fix this?

    the run of my program:
    Enter sales(-1 quit): 5000
    Enter sales (-1 quit):5678
    Salary is 711.00 //should be 711.02
    Also I am having trouble with the precision.

    code:
    main.cpp

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    #include <limits> 
    #include "sales.h"
    using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;
    int main()
    {
        double salary, sales;
        Sales mySales;
        
        
        
        cout<<"Enter sales In dollar(-1 to end): ";
        cin>>sales; //input
    
    // write the loop to get more values
    while(sales!=-1)
    {
            cout<<"Enter sales In dollar(-1 to end): ";
        cin>>sales; //input
    
            mySales.setGrossSales( sales );
            salary = mySales.findSalary();
    
            cout<<std::fixed<<std::setprecision(2)<<"Salary is "<<salary<<endl;//outputs the salary
            //cout;
        }   
    
    }
    sales.h:
    Code:
    class Sales
    {
      public:
    
       // write your constructor here
    
      double getGrossSales()
      {
            return grosssales;
      }
    //this function is the setter
      void setGrossSales( double sales) 
      {
        
       if(sales!=-1)
       {
        
            grosssales=sales;
          
    }
       
          //  write your codes here
      }
      
      double findSalary()
      {
       int salary;
          salary =(0.09 * grosssales) + 200; 
          return salary;
           // calculate and return the salary using grosssales
      }
    
      private:
          double grosssales;
    };
    Last edited by 2kaud; June 16th, 2017 at 06:01 PM. Reason: Added code tags

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,650

    Re: Need help finishing with this c++ project

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    I was able to get the user input the sales, but it does not show the salary right after, but if the user would then input the next it does show the salaries. Any way you can fix this?
    This is because you are asking for sales before the while loop and then also at the beginning of the loop - so inputting the first sales is ignored. Put the cout/cin statements in the while loop after the salary display statement at the end of the loop. Also look at the code in post #3.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)