Roulette

Made a simple roulette with C++
Any advice?
(WORKING CODE)
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
  //
//  main.cpp
//  Keygen
//
//  Created by user on 2015/7/19.
//  Copyright (c) 2015年 Warren Leu. All rights reserved.
//

#include <iostream>
int main() {
/*******************INTRODUCTION******************************************/
//whichbet = Which number user betted on.
//random = Computer's randomly chosen number.
    int money, bet, gamemode, whichbet, random;
    money = 1000;
    std::cout << "This roulette has three betting choices, the chances of winning is 1/2, 1/5, and 1/10.\nIf you win, you will get 2, 5 or 10 times the amount you bet back, depending on the game.\nGamemode 1: 1/2\nGamemode 2: 1/5\nGamemode 3: 1/10\n";
    std::cout << "You have " << ((int)money) << " dollars""\n";
/*************************BETTING*****************************************/
    while(money != 0){
    std::cout << "Gamemode: ";
    std::cin >> gamemode;
        if(gamemode < 1 || gamemode > 3){
            std::cout << "Invalid Number!\n";
            continue;
        }
    std::cout << "Bet Amount: ";
    std::cin >> bet;
    if(bet > money){
        std::cout << "Not Enough Money!\n";
        continue;
        }
    std::cout << "Bet Number: ";
    std::cin >> whichbet;
/*************************RESULT******************************************/
        if(gamemode == 1){
        if(whichbet != 1 && whichbet != 2){
            std::cout << "Invalid Number!\n";
            continue;
        }
    money -= bet;
    //Randomnizer
    srand (time(NULL));
    random = rand() % 2 + 1;
    //Randomnizer
        if(random == whichbet){
            std::cout << "YOU WON!""\n";
            money += bet * 2;
            std::cout << "You have ";
            std::cout << ((int)money);
            std::cout << " dollars""\n";
        }
        else if(random != whichbet){
        std::cout << "YOU LOST!""\n";
            std::cout << "You have ";
            std::cout << ((int)money);
            std::cout << " dollars""\n";
        }
        }
        else if(gamemode == 2){
            if(whichbet < 1 || whichbet > 5){
                std::cout << "Invalid Number!\n";
                continue;
            }
            money -= bet;
            srand (time(NULL));
            random = rand() % 5 + 1;
            if(random == whichbet){
                std::cout << "YOU WON!""\n";
                money += bet*5;
                std::cout << "You have ";
                std::cout << ((int)money);
                std::cout << " dollars""\n";
            }
            else if(random != whichbet){
                std::cout << "YOU LOST!""\n";
                std::cout << "You have ";
                std::cout << ((int)money);
                std::cout << " dollars""\n";
            }
        }
        else if(gamemode == 3){
            if(whichbet < 1 || whichbet > 10){
                std::cout << "Invalid Number!\n";
                continue;
            }
            money -= bet;
            srand (time(NULL));
            random = rand() % 10 + 1;
            if(random == whichbet){
                std::cout << "YOU WON!""\n";
                money += bet*10;
                std::cout << "You have ";
                std::cout << ((int)money);
                std::cout << " dollars""\n";
            }
            else if(random != whichbet){
                std::cout << "YOU LOST!""\n";
                std::cout << "You have ";
                std::cout << ((int)money);
                std::cout << " dollars""\n";
            }
        }

    }
    std::cout << "YOU LOST ALL YOUR MONEY!!";
    return 0;
}

Tried to use functions with this one, but it failed, may be because of int in function is not the same as the int in the main.
Also need help in that department.
(NON-WORKING CODE)
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
//
//  main.cpp
//  Keygen
//
//  Created by user on 2015/7/19.
//  Copyright (c) 2015年 Warren Leu. All rights reserved.
//

#include <iostream>
void check(int random, int whichbet, int bet, int money){
    if(random == whichbet){
        std::cout << "YOU WON!""\n";
        money += bet * 2;
        std::cout << "You have ";
        std::cout << ((int)money);
        std::cout << " dollars""\n";
    }
    else if(random != whichbet){
        std::cout << "YOU LOST!""\n";
        std::cout << "You have ";
        std::cout << ((int)money);
        std::cout << " dollars""\n";
    }
}
int main() {
/*******************INTRODUCTION******************************************/
//whichbet = Which number user betted on.
//random = Computer's randomly chosen number.
    int money, bet, gamemode, whichbet, random;
    money = 1000;
    std::cout << "This roulette has three betting choices, the chances of winning is 1/2, 1/5, and 1/10.\nIf you win, you will get 2, 5 or 10 times the amount you bet back, depending on the game.\nGamemode 1: 1/2\nGamemode 2: 1/5\nGamemode 3: 1/10\n";
    std::cout << "You have " << ((int)money) << " dollars""\n";
/*************************BETTING*****************************************/
    while(money != 0){
    std::cout << "Gamemode: ";
    std::cin >> gamemode;
        if(gamemode < 1 || gamemode > 3){
            std::cout << "Invalid Number!\n";
            continue;
        }
    std::cout << "Bet Amount: ";
    std::cin >> bet;
    if(bet > money){
        std::cout << "Not Enough Money!\n";
        continue;
        }
    std::cout << "Bet Number: ";
    std::cin >> whichbet;
/*************************RESULT******************************************/
        if(gamemode == 1){
        if(whichbet != 1 && whichbet != 2){
            std::cout << "Invalid Number!\n";
            continue;
        }
    money -= bet;
    //Randomnizer
    srand (time(NULL));
    random = rand() % 2 + 1;
    //Randomnizer
            check(random, whichbet, bet, money);
        }
        else if(gamemode == 2){
            if(whichbet < 1 || whichbet > 5){
                std::cout << "Invalid Number!\n";
                continue;
            }
            money -= bet;
            srand (time(NULL));
            random = rand() % 5 + 1;
            check(random, whichbet, bet, money);
            }
        else if(gamemode == 3){
            if(whichbet < 1 || whichbet > 10){
                std::cout << "Invalid Number!\n";
                continue;
            }
            money -= bet;
            srand (time(NULL));
            random = rand() % 10 + 1;
            check(random, whichbet, bet, money);
    }
    }
    std::cout << "YOU LOST ALL YOUR MONEY!!";
    return 0;
}
Last edited on
srand(...) should be called only once at the top of main.

See this:

http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand

for what you need to include.
Topic archived. No new replies allowed.