Is this a good program?

Idk how to explain it, but it seems so messy. I am brand new to programming.

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
#include <iostream>
#include <ctime>    // for time()
#include <cstdlib>
using namespace std;


int main ()
{
    int dieOne=0;
    int dieTwo=0;
    int roll=0;
    int point=0;
    double odds=0;
	double betmoney=50;
    srand(time(0));    //makes time random seed




    for(int game=1; game<4; game++)
    {
        dieOne=(rand()%6)+1;//creates dice
        dieTwo=(rand()%6)+1;
        roll=dieOne+dieTwo;

        cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;

        if(roll==7 || roll==11)//win on first roll
			betmoney+50;

        else if(roll==2 || roll==3 || roll==12)//lose on first roll
            betmoney-50;
        else if(roll==4 || roll==5 || roll==6 || roll==8 || roll==9 || roll==10)
        {
            point=roll;//point set
            do
            {

                dieOne=(rand()%6)+1;//rolls dice again
                dieTwo=(rand()%6)+1;
                roll=dieOne+dieTwo;

                cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;

                if(roll==point)//wins if player rolls point
                    betmoney+50;
                else if(roll==7)//lose if player rolls 7
                    betmoney-50;
                else if(roll!=7 && roll!=point) //nothing otherwise
                    odds=0;

            }while(roll!=point && roll!=7);//keep going until point or 7 is rolled

            cout<<"game over- game #: "<<game<<endl;
			

            }

    }
   
    cout<<"you have:$"<<betmoney<<endl;
    cout<<"Probability to win is: "<<odds<<endl;
}


Is there anything I can do to make it better?
betmoney+50;
This will not do anything.

If you want to increase betmoney by 50 you have to write
betmoney = betmoney + 50;
or
betmoney += 50;
You could use a array.
like instead of:
int dieOne=0;
int dieTwo=0;

You could just do
die[2]

You mention that you're new to programming. Have you just covered basic o/p (i.e. cout) and loops, etc so far??

To improve you code you could (in addition to fixing your maths like Peter87 says):

1. tidy it up a bit (you did say it's messy)

2. simplfy the last if else in both you if statements (if I understand correctly, the last test is for all remaining possible numbers?)

3. you could use a little helper function to make what's going on a bit more obvious?

1
2
3
4
int rollDie()
{
    return (rand()%6)+1;
}


1
2
3
4
5
6
7
8
...

    for(int game=1; game<4; game++)
    {
        dieOne=(rand()%6)+1;//creates dice
        dieTwo=(rand()%6)+1;

    ....


4 Tighten up the scope of some of you variables. e.g. roll is only used inside the for loop.

5. And finally, are you allowed to carry on playing when you've run out of money?

Andy

PS I think Maxim's suggestion about the array is a bit borderline here. Might come in useeful later, though?
Last edited on
Topic archived. No new replies allowed.