The while loop is adding values

Hi all,

I'm creating a dice game where I want the user reach limit of 10 game or game terminates when there is no more money left.

it seems that most of it its working apart from the looping


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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{
cout<<"Hey, welcome!"<<endl;
cout<<"Rules:"<<endl;
cout<<"1.if you get 7 or 11 you win the round!"<<endl;
cout<<"2.if you get 2,3 or 12 you loose"<<endl;
cout<<"3.If you get different number you have to repeat until you get your numer to win unless you get 7 you loose "<<endl;
cout<<"4.press(1) to throw"<<endl;

srand (time(NULL));

        int dice_1,dice_2,result = 0, money = 100;
        int number = 6;
        int diceRoll = 0;
        int roll = 1;
while (1)
{

    cout<<"\nPress '1' to throw"<<endl;
    cin>>diceRoll;

dice_1 = rand() % number + 1;
dice_2 = rand() % number + 1;
result = dice_1 + dice_2;


        int round = 0;
while (money <=0 || round ==10);
        if (round <=0);
{


int points = 0;
cout<< "Your score:"<<result<<endl;
if (roll == 1)
{


                                 if (result == 2 || result == 3 || result == 12)
                                 {
                                money = money - 30;
                                round++;
                                 }
                                 else if (result == 7 || result == 11)
                                 {
                                money= money+ 20;
                                round++;
                                 }
                                 else
                                 {
                                points = result;
                                roll = 2; 
                                 }
                
                                cout<< "You have:"<<money<<endl;
                                cout<< "points:"<<points<<endl;
}
if (roll == 2) 
{
cout<<"\n\n fight for your money!";

if (result == 7)
{
    money = money - 30;
    result = 0;
    round++;
    roll= 1;
}
else if (points = result)
{
    money= money + 20;
    points= 0;
    round++;
    roll = 1;
}
else
{
    money = money;
}
}





}
}
    return 0;
}


it seems that the money in second part keep adding and the loop does not finish at round 10. I don't know hat i did wrong...
Last edited on
Look real careful at your while on line 34.

That semicolon at the end of the statement. Everything that follows that should be a part of the while block isn't.

Your code formatting could use a bit of work as well. :)
Also L35


My parser stopped when it errored out on line 34. :Þ

Yeah, lines 34 & 35 aren't doing what you think they are doing, courtesy of the semicolons terminating the controlled blocks without doing anything.

The else clause at 82-85 is kinda irrelevant with money = money;. Nothing would change if you eliminated it entirely.
ok, I have change a bit to swich and cases:
sorry the code has a bit of polish, but anyway i hope you'll get it.
(by the way ths is my first week of learning)
I don't know what to do with case 4

I want from else to move to the beginning to case 4 until I'll get 4 or 7 (to determine if use looses or wins)

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
switch (wynik)
        {
        case 2:
                cout<< "PRZEGRALES: ODDAJ 30ZL" << endl;
                pieniadzeGRACZA1 = pieniadzeGRACZA1 - 30;
                runda = runda + 1;
                wynik = 0;
                cout<<"Zostalo ci: "<<pieniadzeGRACZA1<<"zl"<<endl;
                break;

        case 3:
                cout<< "PRZEGRALES: ODDAJ 30ZL" << endl;
                pieniadzeGRACZA1 = pieniadzeGRACZA1 - 30;
                runda = runda + 1;
                wynik = 0;
                cout<<"Zostalo ci: "<<pieniadzeGRACZA1<<"zl"<<endl;
                break;
        case 4 :
                cout<<"Graj dalej: twoja suma to 4"<< endl;
            if (wynik = 4)
            {
                cout<< "WYGRALES: DOSTAJESZ 20ZL" << endl;
                pieniadzeGRACZA1 = pieniadzeGRACZA1 + 20;
                runda = runda + 1;
                wynik = 0;
                cout<<"Zostalo ci: "<<pieniadzeGRACZA1<<"zl"<<endl;
            break;
            }
            else if (wynik = 7)
            {
                cout<< "PRZEGRALES: ODDAJ 30ZL" << endl;
                pieniadzeGRACZA1 = pieniadzeGRACZA1 - 30;
                runda = runda + 1;
                wynik = 0;
                cout<<"Zostalo ci: "<<pieniadzeGRACZA1<<"zl"<<endl;
                break;
            }
            else
            {
                cout<<"RZUC JESZCZE RAZ" << endl;
            continue;
            }
Topic archived. No new replies allowed.