How could this be better?

I want to use functions wherever possible in my code below, but to be honest I don't know how to use functions without using global variables which I do NOT want to do. Any tips/pointers/examples of what to do sampling my code? Here it is below.

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std; 


int main()
{
//set variables
int choice;
double selection;
int where_drop = 0;
double user_winnings;
int randomizer;
int multiple_chips;
double selection_two;
int randomizer_two;
double user_winnings_two;
double average_winnings;
bool gameOn = true;


//make menu screen
while (gameOn != false){
cout << "***GAME MENU***\n";
cout << " 1 - Drop one chip into one slot.\n";
cout << " 2 - Drop multiple chips into one slot.\n";
cout << " 3 - Drop multiple chips into every slot. \n";
cout << " 4 - Exit.\n";
cout << " Enter your choice and press enter: ";
cin >> choice;

//menu choices below in switch
switch (choice)
{
case 1:
	
	cout << "\n Which Slot would you like to Drop a chip in(Please enter a slot #0-8)?"<<"\n";
	cin >> selection;

if((selection < 0) || (selection > 8))//if a negative number or greater than 8 go back to menu
{
	break;
}

srand(time(0));  //seed the random number
for(int i=1; i<=12; i++ ) //12 iterations 
{
  randomizer = rand() % 2;
  
  if (randomizer == 0)
  {
	  
	selection += ( selection == 8  ) ?  -0.5 : 0.5;
  } 
  else
  {
	selection += ( selection == 0  ) ?  0.5 : -0.5;
  }

  cout << selection << endl; //print out the 12 drops of plinko
}

//find winnings. could of done array here.

int user_winnings[9] = {
  100,
  500,
  1000,
  0,
  10000,
  0,
  1000,
  500,
  100
};


//print out winnings
	cout<<"You've won $" << user_winnings << "!" << endl;

break;
case 2: //second option

cout << "\n How many chips would you like to play?"<<"\n";
cin >> multiple_chips;

if(multiple_chips < 0)//don't allow negative numbers
{
	break;
}

cout << "\n Which Slots would you like to Drop the chip(s) into(Please enter a slot #0-8)?"<<"\n";
cin >> selection_two;

if((selection_two < 0) || (selection_two > 8))//only allow 0-8 as option or return to menu
{
	break;
}

srand(time(0));  //seed the random number
for(int i=1; i<=12; i++) //12 iterations starting from 1
{
  randomizer_two = rand() % 2;
  if (randomizer_two == 0)
  {
	selection_two += ( selection_two == 8  ) ?  -0.5 : 0.5;
	
  } 
  else
  {
	selection_two += ( selection_two == 0  ) ?  0.5 : -0.5; 
 	
  }
}

//find winnings. could of done array here.
int user_winnings_two[9] = {
  100,
  500,
  1000,
  0,
  10000,
  0,
  1000,
  500,
  100
};


//final calculations
average_winnings = user_winnings_two / multiple_chips;

	cout<<"You've won  a total of $" << user_winnings_two << "!" << endl;
	cout<<"You averaged winnings of $" << average_winnings << " a chip." << endl;

break;

case 4://quit program
cout << "End of Program.\n";
gameOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
break;
cin >> choice;

}

}
return 0;
}
You can make every case a function. For example
1
2
3
4
5
6
7
8
9
10
switch (choice)
{
case 1:
  OneChipToOneSlot();
  break;
case 2:
  MultiChipToOneSlot();
  break;
// etc
}


It doesn't look like you need to exchange data between main and the subroutines, since all you do is write to cout

The variables in main that you use inside the cases can be defined inside the functions that will substitute the cases
Example:
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
void OneChipToOneSlot()
{	
  double selection;
  double user_winnings; // By the way, I can't see this being initialized
  int randomizer;
  cout << "\n Which Slot would you like to Drop a chip in(Please enter a slot #0-8)?"<<"\n";
  cin >> selection;

  if((selection < 0) || (selection > 8))
  {
    return;
  }

  srand(time(0));
  for(int i=1; i<=12; i++ )
  {
    randomizer = rand() % 2;
  
    if (randomizer == 0)
    {
	  
      selection += ( selection == 8  ) ?  -0.5 : 0.5;
    } 
    else
    {
      selection += ( selection == 0  ) ?  0.5 : -0.5;
    }

    cout << selection << endl;
  }

int user_winnings[9] = {
  100,
  500,
  1000,
  0,
  10000,
  0,
  1000,
  500,
  100
};


	cout<<"You've won $" << user_winnings << "!" << endl; // Outputting uninitialized variable
}


It seems to me that there are some probelms with the code, but looking at other people's code always confuses me, so I might have missed something
Last edited on
use functions without using global variables

Yeah, thats a great decision. You simply pass the variables you need to the functions. For example your ShowMenu() function:
1
2
3
4
5
6
7
8
9
10
11
int ShowMenu() {
    cout << "***GAME MENU***\n";
    cout << " 1 - Drop one chip into one slot.\n";
    cout << " 2 - Drop multiple chips into one slot.\n";
    cout << " 3 - Drop multiple chips into every slot. \n";
    cout << " 4 - Exit.\n";
    cout << " Enter your choice and press enter: ";
    cin >> choice;

    return choice;
}

Now in your switch you can use: switch( ShowMenu() )
This function simply outputs text, gets input and returns it, so no variables from main() are necessary. Another example, where those variables áre necessary:
1
2
3
4
5
6
void FinalCalculations( double user_winnings_two, double multiple_chips ) {
    double average_winnings = user_winnings_two / multiple_chips;

    cout<<"You've won  a total of $" << user_winnings_two << "!" << endl;
    cout<<"You averaged winnings of $" << average_winnings << " a chip."<< endl;
}

Each function deals with a little part of the program, and so needs some of the variables declared in main() . And you give them that variables by passing those variables to them.
Just remember to define choice inside SHowMenu() before using it to store the input
yes, thats right, I forgot
I just am really new to this. Would I just put for example


OneChipSlot();

in my code and that's it(after putting the void OneChipSlot etc above MAIN)? Seems kind of dumb ha.
Last edited on
For small programs you usually write function prototypes before main, and the function definition after main. The prototypes consist of the return type, the name of the function and the parameters that it takes
So, for example

1
2
3
4
5
6
7
8
9
10
11
12
13
void OneChipSlot(); // Prototype. It returns void and doesn't take parameters

int main()
{
// Stuff
  OneChipSlot();
// Other stuff
}

void OneChipSlot() // Definition. Here you write what the function actually does
{
  std::cout << "OneChipSlot() was called" << std::endl;
}

The prototype is needed to tell the compiler that the function exists. You can also define a function in its prototype, but thats usually not recommended
Topic archived. No new replies allowed.