Can't get this code working... My head hurts

So this is an assignment for my compSci class.
Basically it's a program to calculate commission by determining sale amount and
whether you are a salesperson or loan broker. It's working if you don't misspell
"salesperson" or "loan broker" the first time. After re-verifying, it flips out on me.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

int person();
double commission();

int main() {

	double money = 0;
	string name;
	cout << "Hello!\nThis program will tell you how much commision you will earn for a sale." << endl <<
		"Can I have your name please? ";
	getline(cin, name);
	cout << endl << "Hi " << name <<", ";
	money = commission(); //amount earned in commisions

	cout << endl << endl <<
		"The amount you will earn in commisions is: $" << setprecision(2) << fixed << money << endl <<
		"Have a nice day!" << endl <<endl ;

	system("pause");

	return 0;
		
}

int person() {

	string salesperson;
	int personType; //Type1 is salesperson, Type2 is loan broker
	cout << "Are you a salesperson or a loan broker? ";
	getline(cin, salesperson);
	if (salesperson == "salesperson" || salesperson == "Salesperson") {
		personType = 1;
	}
	else if (salesperson == "loan broker" || salesperson == "Loan broker" || salesperson == "Loan Broker") {
		personType = 2;
	}
	else {
		cout << "You have entered an illogial response.\nPlease try again." << endl << endl;
		person();
	}
	return personType;
}

double commission() {

	double sale; //Sale amount
	string mystr;
	cout << "how much was your sale? ";
	getline(cin, mystr);
	stringstream(mystr) >> sale;
	double amount = 0; //Amount earned in commisions

	int Type = person(); //Determining if salesperson or loan broker

	if (Type == 1) {
		if (sale <= 15000) {
			amount = sale * 0.03;
		}
		else if (sale > 15000 && sale <= 25000) {
			amount = sale * 0.04;
		}
		else if (sale > 25000){
			amount = sale * 0.05;
		}
	}
	else if (Type == 2) {
		if (sale <= 15000) {
			amount = sale * 0.005;
		}
		else if (sale > 15000 && sale <= 25000) {
			amount = sale * 0.008;
		}
		else if (sale > 25000){
			amount = sale * 0.01;
		}
	}
	return amount;
}


Any help would be greatly appreciated!
Last edited on
Your retrying function doesn't work. Recursively calling the function is a) bad in this case, and b) will not do anything for you since you are throwing away the return value.

If you must recurse, you should be using the value you got from the recursive call. Otherwise, use a loop.
On line 45 you don't assign the return value of person() to anything, so by line 47 personType may be uninitialized.
The correct solution would be not to use recursion for looping.
Oh god... so it's all bad?
I spent way too much time on this piece of crap :/

Ok, so if I use a do-while loop and for the condition,

while (salesperson != "salesperson" && salesperson = "Salesperson")

does a comma work to check if salesperson for both the strings.
(i.e):

while (salesperson != "salesperson" , "Salesperson")

or no?

Anyways, thanks
Last edited on
does a comma work to check if salesperson for both the strings.
(i.e):

while (salesperson != "salesperson" , "Salesperson")
No.
while (salesperson != "salesperson" && salesperson = "Salesperson")


Correction:
while (salesperson != "salesperson && salesperson == "Salesperson")
Topic archived. No new replies allowed.