Most effecient way to code this with functions?

Hi, I am starting to write more programs for my own use, this program has no real user input validation but it doesn't matter as its only me using it and I am pretty much writing it for the sake of practicing. My code works just fine, but I want to be able to write it the most efficiently I can to get into the habbit of writing properly.

Basically, user selects an input from the menu 1-4, inputs "roi" inputs "games" and the program spits out the results.

Would I be better off using a switch statement and cases for $60,$100,$200,$300 and each case just passes the bi_level and the correct "cout" strings $60,$100 etc... to a function that processes the result? etc?

Hope that makes sense , any help appreciated!,

Cheers,

Ben.

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
#include <iostream>

using namespace std;



int main ()
{
	double roi = 0;
	int games = 0;
	int input = 0;
	double rakeb_60 = 0.5292;
	double rakeb_100 = 0.7896;
	double rakeb_200 = 1.4028;
	double rakeb_300 = 1.8858;
	double bi_level = 0;
	double result = 0;
	char recalc = 'y';

	cout << "\t\tProjected Earnings Calculator\n\n";

	while ( recalc == 'y' )
	{
	
	cout << "1. $60\n" ;
	cout << "2. $100\n" ;
	cout << "3. $200\n" ;
	cout << "4. $300\n\n" ;
	cout << "What buy-in level do you want to calculate: ";
	cin >> input;

	cout << "\nEnter your projected roi: " ;
	cin >> roi;
	cout << "\nNumber of games: ";
	cin >> games;


	if ( input == 1 )
	{
		bi_level = 60;
		result = bi_level / 100 * roi * games;
		cout << "\n\n*** Projected earnings for $60 bi with % " << roi << "roi, over " << games << " games ***\n\n";
		cout << "$" << result << "\n" << "$" << rakeb_60 * games << " @ 42% RB" <<  "\n" << "$" << rakeb_60 * games + result << " Total inc RB" << "\n\n";
	}
	if ( input == 2 )
	{
		bi_level = 100;
		result = bi_level / 100 * roi * games;
		cout << "\n\n*** Projected earnings for $100 bi with % " << roi << "roi, over " << games << " games ***\n\n";
		cout << "$" << result << "\n" << "$" << rakeb_100 * games << " @ 42% RB" <<  "\n" << "$" << rakeb_100 * games + result << " Total inc RB" << "\n\n";
	}
	if ( input == 3 )
	{
		bi_level = 200;
		result = bi_level / 100 * roi * games;
		cout << "\n\n*** Projected earnings for $200 bi with % " << roi << "roi, over " << games << " games ***\n\n";
		cout << "$" << result << "\n" << "$" << rakeb_200 * games << " @ 42% RB" <<  "\n" << "$" << rakeb_200 * games + result << " Total inc RB" << "\n\n";
	}
	if ( input == 4 )
	{
		bi_level = 300;
		result = bi_level / 100 * roi * games;
		cout << "\n\n*** Projected earnings for $300 bi with % " << roi << "roi, over " << games << " games ***\n\n";
		cout << "$" << result << "\n" << "$" << rakeb_300 * games << " @ 42% RB" <<  "\n" << "$" << rakeb_300 * games + result << " Total inc RB" << "\n\n";
	}

		cout << "Make another calculation? Y/N: ";
		cin >> recalc;
		if ( recalc == 'n')
		{
			break;
		}
	}
	return 0;

}
The biggest tip I can give you is, when possible/reasonable, combine code into a common function rather than copy/pasting it over and over. Each of your four if statements are pretty much identical apart from a few variables. So stuff all that code in a function and pass the differences in as parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void calcAndShowResults( int bi_level, double rakeb, int games, double roi )
{
    double result = bi_level / 100.0 * roi * games;
    cout << "\n\n*** Projected earnings for $" << bi_level << " bi with % " << roi << "roi, over " << games << " games ***\n\n";
    cout << "$" << result << "\n" << "$" << rakeb * games << " @ 42% RB" <<  "\n" << "$" << rakeb * games + result << " Total inc RB" << "\n\n";
}

int main()
{
//....
         if( input == 1 )    calcAndShowResults(  60, rakeb_60 , games, roi );
    else if( input == 2 )    calcAndShowResults( 100, rakeb_100, games, roi );
    else if( input == 3 )    calcAndShowResults( 200, rakeb_200, games, roi );
    else if( input == 4 )    calcAndShowResults( 300, rakeb_300, games, roi );
Last edited on
Disch,

I knew a function would be alot easier and effecient but I really wasn't sure about the best way of implementing it and I wasn't sure how to pass variable inputs to a function correctly which you displayed very easily. This is a huge help thanks alot for taking the time, really appreciate it.
One quick final question actually :) , this is the new code, is there anyway to initialise the variables within the if statements themselves and if so is this generally seen as bad practice? Does the new code look as clean as it could be or have I still made mistakes?

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
#include <iostream>
using namespace std;

void calcAndShowResults( int bi_level, double rakeb, int games, double roi )
{
    double result = bi_level / 100.0 * roi * games;
    cout << "\n\n*** Projected earnings for $" << bi_level << " bi with %" << roi << "roi, over " << games << " games ***\n\n";
    cout << "$" << result << "\n" << "$" << rakeb * games << " @ 42% RB" <<  "\n" << "$" << rakeb * games + result << " Total inc RB" << "\n\n";
	
}

int main()
{
	double roi = 0;
	int games = 0;
	int input = 0;
	double rakeb_60 = 0.5292;
	double rakeb_100 = 0.7896;
	double rakeb_200 = 1.4028;
	double rakeb_300 = 1.8858;
	double bi_level = 0;
	double result = 0;
	char recalc = 'y';

	cout << "\t\tProjected Earnings Calculator\n\n";

	while ( recalc == 'y' )
	{
	cout << "1. $60\n" ;
	cout << "2. $100\n" ;
	cout << "3. $200\n" ;
	cout << "4. $300\n\n" ;
	cout << "What buy-in level do you want to calculate: ";
	cin >> input;

	cout << "\nEnter your projected roi: " ;
	cin >> roi;
	cout << "\nNumber of games: ";
	cin >> games;

	if( input == 1 )    calcAndShowResults(  60, rakeb_60 , games, roi );
	else if( input == 2 )    calcAndShowResults( 100, rakeb_100, games, roi );
	else if( input == 3 )   calcAndShowResults( 200, rakeb_200, games, roi );
	else if( input == 4 )    calcAndShowResults( 300, rakeb_300, games, roi );
	
	cout << "Make another calculation? Y/N: ";
	cin >> recalc;
	if ( recalc == 'n')
		{
			break;
		}
	}

}
up, any1 else see anyway this can be improved or it looks fine?
Topic archived. No new replies allowed.