### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153`` ``````#include #include #include 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
 ``12345678910`` ``````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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````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:
 ``1234567891011`` ``````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:
 ``123456`` ``````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

 ``12345678910111213`` ``````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.