Better way?

I'm currently in school studying C++ and on my current project the goal is:
"Use the switch statement to determine the commission. If the sales are less than zero, display the message "The sales cannot be less than 0." If the code is not 1, 2, or 3, display the message "Invalid code" and don't ask the user for a sales amount."

I came up with two different sets of code, I was primarily wondering which one was better for me to turn in. They both work and I know there is more than one way to do this, I was just wondering which one was better and why. TIA

I'm thinking the first one, just because it's 8 lines less of code and there is less repetition. Just need a second opinion I guess. Again TIA.

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
// Lab6-3.cpp - displays a salesperson's commission
// Created/revised by  on 11 Sept. 2012

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	// Declare variables
	int sales			= 0;
	int code			= 0;
	double commission	= 0.0;

	// Enter input
	cout << "Code: ";
	cin >> code;

	// Checks for correct code entry
	if (code != 1 && code != 2 && code !=3)
	{
		cout << "Invalid Code";
	}
	else
	{
		cout << "Sales: ";
		cin >> sales;
	}	//end if
	
	// Checks for correct sales amount
	if (sales < 0)
	{
		cout << "The sales cannot be less than 0.";
	}
	else
	{
		// Determine commission
		switch(code)
		{
		case 1:
				commission = sales * .02;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
		case 2:
				commission = (sales - 100000) * .05 + 2000;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
		case 3:
				commission = (sales - 400000) * .1 + 17000;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
		}	// end switch
	}	//end if
	cout << endl;
	//system("pause")
	return 0;
}	// End of main function 


OR

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
// Lab6-3.cpp - displays a salesperson's commission
// Created/revised by  on 11 Sept. 2012

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	// Declare variables
	int sales			= 0;
	int code			= 0;
	double commission	= 0.0;

	// Enter input
	cout << "Code: ";
	cin >> code;

	// Determine commission
	switch(code)
	{
	case 1:
			cout << "Sales: ";
			cin >> sales;
				if (sales < 0)
				{
					cout << "The sales cannot be less than 0.";
					break;
				}
				else
				commission = sales * .02;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
	case 2:
			cout << "Sales: ";
			cin >> sales;
			if (sales < 0)
				{
					cout << "The sales cannot be less than 0.";
					break;
				}
				else
				commission = (sales - 100000) * .05 + 2000;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
	case 3:
			cout << "Sales: ";
			cin >> sales;
			if (sales < 0)
				{
					cout << "The sales cannot be less than 0.";
					break;
				}
				else
				commission = (sales - 400000) * .1 + 17000;
				cout << fixed << setprecision(2);
				cout << "Commission: $" << commission;
				break;
	default:
			cout << "Invalid code";
	}		// end switch

	cout << endl;
	//system("pause")
	return 0;
}	// End of main function 
The first one looks much better, mainly because there is less repetition. Nice job!
Last edited on
Out of those 2 options i prefer the 1st version.

The main difference is in the way that the user's input is validated. With version 1, the user's input is validated completely separate of of what being validated means.
With version 2, they are mixed together making it harder to upgrade the code in the future --- imagine having to add a more complicated validation, like username and password ? or network authentication...it'd be a mess to have everything mish mashed eh ? XD
soranz said it really well. That's the more precise reason that I preferred it, I just didn't have a good way to say it. Less repetition was the simple concept.

The idea that separate pieces of code handle different things should carry through to classes and functions too. Each class should handle one thing and each function within that class should handle one thing. Another benefit, besides the one that soranz mentioned, is that this can help to you write code that is reusable within other projects, which can be a real time saver.
Thanks monad and nice addon too. Good design leads to reusable code.
I appreciate the responses, very informative, exactly what I was looking for. Great points made and I will definitely take it with me to future programs. Will for sure frequent this site at least throughout the remainder of this class.

Thanks for the great responses!
Topic archived. No new replies allowed.