Currently working through the Jumping Into C++ E-book, on the chapter about randomizing, one of the practice problems has asked me to make a "slot machine" style output and then have rewards for different results. The code below is what I have come up with, but I know it can be better. First question : the system("cls") I have read around and realize that system calls are often frowned upon, BUT I honestly can't think of a way to get this to "look like a slot machine" without clearing the window out each time, any ideas on how to do this without the system call? It does really slow it down and would be nice to be rid of... Second question : any ideas on how I could write the code better(shorter) for the rewards? I wish switches could take in multiple variable dependencies but alas they cannot :/.
Code:#include <iostream> #include <stdlib.h> #include <ctime> using namespace std; int randNum0 (int low, int high) { return rand() % (high-low) + low; } int randNum1 (int low, int high) { return rand() % (high-low) + low; } int randNum2 (int low, int high) { return rand() % (high-low) + low; } int main() { int num0; int num1; int num2; for (int i = 0; i < 100; ++i) { srand (time(NULL)); int num0 = randNum0(1, 9); int num1 = randNum1(1, 9); int num2 = randNum2(1, 9); system("CLS"); cout << "-------" <<endl; cout << "|" << num0 << "|" << num1 << "|" << num2 << "|\n"; cout << "-------" <<endl; } if (num0 == 1 && num1 == 1 && num2 == 1) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 2 && num1 == 2 && num2 == 2) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 3 && num1 == 3 && num2 == 3) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 4 && num1 == 4 && num2 == 4) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 5 && num1 == 5 && num2 == 5) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 6 && num1 == 6 && num2 == 6) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 7 && num1 == 7 && num2 == 7) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 8 && num1 == 8 && num2 == 8) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } if (num0 == 9 && num1 == 9 && num2 == 9) { cout << "You won " <<num0 <<"dollars. Congratulations!\n"; } }



12Likes
LinkBack URL
About LinkBacks




Without that performance penalty, the program will run so fast you won't get to see anything! So you'll need to introduce a delay.