Please help

Okay so my assignment is as follows:
Write a program to calculate a salespersons commission based upon their monthly sales volume and
years of service. The program should prompt the user for the salespersons name, monthly sales volume, and
years of service. The output should display each of the input values along with the commission rate and
calculation (commission = rate * sales). Use constants where appropriate, suitable data types, and format the
output in fields with 2 decimal precision. Use the rates from the following table when determining the
commission rate. The one exception with the commission calculation is for the owner’s son in-law, “Bob
Freeloader”. His commission rate is always 50% higher than the other employees for a given level of sales and
years of service.

And my code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include<iostream>
#include<iomanip>
#include<cstring>
#include<string>

using namespace std;

int main()
{
  // char name[80];
   double sales;
   int years;

   double commissionRate=0;
   double commission=0;

   std::string name;
   std::cout<<"Please enter the employee name: ";
   std::getline(std::cin, name);

   cout<<"Please enter the monthly sales volume: ";
   cin>>sales;

   cout<<"Please enter employees years of service: ";
   cin>>years;


   if(years<10)
   {
       if((name,"Bob Freeloader")==0 && sales<50000)
           commissionRate=0.03;
       else if((name,"Bob Freeloader")==0 && sales>=50000)
           commissionRate=0.06;
       else if((name,"Bob Freeloader")!=0 && sales<50000)
           commissionRate=0.02;
       else
           commissionRate=0.04;
   }
   else
   {
       if((name,"Bob Freeloader")==0 && sales<50000)
           commissionRate=0.12;
       else if((name,"Bob Freeloader")==0 && sales>=50000)
           commissionRate=0.18;
       else if((name,"Bob Freeloader")!=0 && sales<50000)
           commissionRate=0.08;
       else
           commissionRate=0.12;
   }

   cout<<endl;

   cout<<left<<setw(20)<<"Employee :"<<right<<setw(10)<<name<<endl;
   cout<<left<<setw(20)<<"Sales Volume :"<<right<<setw(10)<<sales<<endl;
   cout<<left<<setw(20)<<"Service Years :"<<right<<setw(10)<<years<<endl;
   cout<<left<<setw(20)<<"Commission Rate :"<<right<<setw(10)<<commissionRate<<endl;
   commission=sales*commissionRate;
   cout<<left<<setw(20)<<"Commission :"<<right<<setw(10)<<commission<<endl;

   return 0;

}

Now, it runs fine and I can input and get outputs, but whether i put in the name Bob Freeloader or any other name, the output is the same except that Bob's commission rate is supposed to be 50% higher than everyone else's. I cannot figure out why it will not work the way I need it to. Thank you
if((name,"Bob Freeloader")==0
WTF is this? This looks like someone showed you a C function for comparing char arrays, and you kind of half-remembered something about it.

Here is how to compare a C++ string with a string literal:
if (name == "Bob Freeloader")
Last edited on
I had it like this
 
if(strcmp(name,"Bob Freeloader")==0

but it clashed with the
1
2
3
std::string name;
   std::cout<<"Please enter the employee name: ";
   std::getline(std::cin, name);

Any idea on how to fix this? This is my midterm lab and its due by midnight, i shouldn't procrastinate anymore
strcmp is a C fnuction for comparing char arrays.
You aren't using char arrays. You're using C++ strings.

Remove the header cstring. Don't use strcmp.
Please enter the employee name: Bob Freeloader
Please enter the monthly sales volume: 20000
Please enter employees years of service: 15

Employee : Bob Freeloader
Sales Volume : 20000
Service Years : 15
Commission Rate : 0.08
Commission : 1600

Please enter the employee name: Jessica Whywontthiswork
Please enter the monthly sales volume: 20000
Please enter employees years of service: 15

Employee : Jessica Whywontthiswork
Sales Volume : 20000
Service Years : 15
Commission Rate : 0.08
Commission : 1600


Those my two outputs for the exact same input. Bob's should be a .12 commision rate though. I removed the cstring, yet I still cannot get it to output what I need it to
So now how are you comparing the input string with "Bob Freeloader"?
I changed it to if((name,"Bob Freeloader")!=0 && sales<50000)

but still no dice, what do you mean how am I comparing them?
Let's examine the condition in this if statement.

((name,"Bob Freeloader")!=0 && sales<50000

What exactly does (name, "Bob Freeloader") do?

That's a bit of a trick question, actually. It doesn't do anything and I have no idea why you've got it there. it's crazy. it seems that you had this:

strcmp(name, "Bob Freeloader") which is an attempt to call the function strcmp, okay, not correct but i can see what you're trying to do, and then you replaced it with this: (name, "Bob Freeloader") which is crazy. What kind of programming is it where removing the function name somehow makes it work?


Anyway, if you look back in the thread, to the first response, I already told you how to compare the input string to "Bob Freeloader".
Okay so I completely overlooked that and updated my code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
  // char name[80];
   double sales;
   int years;

   double commissionRate=0;
   double commission=0;

   std::string name;
   std::cout<<"Please enter the employee name: ";
   std::getline(std::cin, name);

   cout<<"Please enter the monthly sales volume: ";
   cin>>sales;

   cout<<"Please enter employees years of service: ";
   cin>>years;


   if(years<10)
   {
       if((name == "Bob Freeloader") && sales<50000)
           commissionRate=0.03;
       else if((name == "Bob Freeloader")&& sales>=50000)
           commissionRate=0.06;
       else if((name == "Bob Freeloader")&& sales<50000)
           commissionRate=0.02;
       else
           commissionRate=0.04;
   }
   else
   {
       if((name == "Bob Freeloader")&& sales<50000)
           commissionRate=0.12;
       else if((name == "Bob Freeloader")&& sales>=50000)
           commissionRate=0.18;
       else if((name == "Bob Freeloader") && sales<50000)
           commissionRate=0.08;
       else
           commissionRate=0.12;
   }

   cout<<endl;

   cout<<left<<setw(20)<<"Employee :"<<right<<setw(10)<<name<<endl;
   cout<<left<<setw(20)<<"Sales Volume :"<<right<<setw(10)<<sales<<endl;
   cout<<left<<setw(20)<<"Service Years :"<<right<<setw(10)<<years<<endl;
   cout<<left<<setw(20)<<"Commission Rate :"<<right<<setw(10)<<commissionRate<<endl;
   commission=sales*commissionRate;
   cout<<left<<setw(20)<<"Commission :"<<right<<setw(10)<<commission<<endl;

   return 0;

}

but I am still having the same problem
The code now works; you've just got the logic plain wrong.

Bob's commission rate is supposed to be 50% higher than an employee with the same years and sales.

So how come it's possible for a normal employee to get a commission of, for example, 0.12 and Bob to get a commission of 0.8 for the exact same sales/years? Shouldn't Bob be making 50% MORE?

Take the top set of figures. Normal employee makes 0.04 commission. 50% more is 0.06, so how come Bob makes 0.02 or 0.03 or 0.06 for the same thing?

Bob's commission depends on years AND sales. Everyone else's commission only depends on years.
Last edited on
Got it to execute flawlessly now. Revised code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
   //char name[80];
   double sales;
   int years;

   double commissionRate=0;
   double commission=0;

   std::string name;
   std::cout<<"Please enter the employee name: ";
   std::getline(std::cin, name);

   cout<<"Please enter the monthly sales volume: ";
   cin>>sales;

   cout<<"Please enter employees years of service: ";
   cin>>years;


   if(years<10)
   {
       if((name == "Bob Freeloader") && sales < 50000)
           commissionRate = 0.03;
       else if((name == "Bob Freeloader") && sales>=50000)
           commissionRate = 0.06;
       else if(sales < 50000)
    	   commissionRate = 0.02;
       else if(sales >= 50000)
    	   commissionRate = 0.04;
   }
   else
   {
       if((name == "Bob Freeloader") && sales < 50000)
           commissionRate = 0.12;
       else if((name == "Bob Freeloader") && sales >= 50000)
           commissionRate = 0.18;
       else if(sales < 50000)
           commissionRate = 0.08;
       else if(sales >= 50000)
    	   commissionRate = 0.12;
   }

   cout<<endl;

   cout<<left<<setw(20)<<"Employee :"<<right<<setw(10)<<name<<endl;
   cout<<left<<setw(20)<<"Sales Volume :"<<right<<setw(10)<<sales<<endl;
   cout<<left<<setw(20)<<"Service Years :"<<right<<setw(10)<<years<<endl;
   cout<<left<<setw(20)<<"Commission Rate :"<<right<<setw(10)<<commissionRate<<endl;
   commission=sales*commissionRate;
   cout<<left<<setw(20)<<"Commission :"<<right<<setw(10)<<commission<<endl;

   return 0;

}

Thanks for your help
Last edited on
Topic archived. No new replies allowed.