patterncppMinor
Virtual Slots Implementation
Viewed 0 times
implementationslotsvirtual
Problem
What I can do to make this code more efficient?
It is supposed to be like one of those slot machines where you gamble on it and stuff (like at a casino).
It is supposed to be like one of those slot machines where you gamble on it and stuff (like at a casino).
#include
#include
#include
#include
#include
using namespace std;
int main() {
srand(time(0));
int slot1;
int slot2;
int slot3;
double total_money = 0;
double total_wins = 0;
double total_tries = 0;
double win_percentage;
char play_again;
cout > play_again;
system("PAUSE");
system("CLS");
} while (play_again != 'N'); // Keep going until user says No
cout << "See you next time!" << endl;
_getch();
return 0;
}Solution
The usual improvements we often suggest here on Code Review:
Using namespace:
Try not to
Or don't do any of that and prefix library calls with
The
Windows extensions:
Using namespace:
Try not to
using namespace std. Read a discussion about this here. Instead, just explicitly declaring the elements you are using would be better:using std::cout;
using std::cin;
... etc...Or don't do any of that and prefix library calls with
std::. Not that much more typing anyway.system("PAUSE") & friends:The
system() function asks the OS to run whatever program you specify as its argument. It just so happens that on Windows there are two programs in the default path called "PAUSE" and "CLS". This is not portable. On Unix those programs do not exist. But the biggest evil of system() is that it is a major security hole. For example, if a hacker manages to replace "CLS" with an exploit program, your program is going to be the one who fires it. So no serious code outside a learning demo should use those. Be aware of this issue.Windows extensions:
_getch() and ` are Windows extensions. You can replace _getch() with the standard std::cin.get().
C++11 library:
The new standard has introduced a more powerful pseudo-random number generation library: . This library should be used instead of rand(), srand() for new code.
Funky if:
This
if (slot1 == slot2 == slot3)
As was noted in the comments, is nonsense. You have to use the &&` (AND) operator:if ((slot1 == slot2) && (slot2 == slot3)) { // If all three slots match, user getx 3x moneyCode Snippets
using std::cout;
using std::cin;
... etc...if (slot1 == slot2 == slot3)if ((slot1 == slot2) && (slot2 == slot3)) { // If all three slots match, user getx 3x moneyContext
StackExchange Code Review Q#69907, answer score: 7
Revisions (0)
No revisions yet.