please help getting a game to go from end to the start again using while loops,boolean

Hi guys i need help with getting my dice game to go back to the beginning of the game after the last line of code in the end. I dont understand what is wrong, been working on it for a while now. Im using the while loops and as i have understood it they continue looping until they are false.
Can anyone make me understand how i can get my game to start again, that is going from part3 to part 1 ? i tried with writing part1=true after the end of the game but it has not worked?

here is my code for the dice game,

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  
#include <ctime>         
#include <cstdlib>        
#define MAX(dice_one,dice_two) ( (dice_one) > (dice_two) ? (dice_one) : (dice_two) ) //lagrar och räknar ut största talet 
#define MAXA(dice_three,dice_four) ( (dice_three) > (dice_four) ? (dice_three) : (dice_four) )
using namespace std;
int main()

{
    int GAME_account=0;
    int dice_one;
    int dice_two;
    int dice_three;
    int dice_four;
    int GAME_bet=0;
    int points_user=0;
    int points_computer=0;
    char answer;
    bool part1 = true;
    bool part2 = true;
    bool part3 = true;
    srand (time(0));
    
    
   
    
        cout << "Welcome to the dice game!" << endl;
    
        cout << "Press p to play and e to exit" << endl;
        cin>> answer;
        
        if (svar == 'p')
            
        {
            
            part1=true;
        }
        
        else if (answer=='e')
            
        {
            cout<< "game over"<<endl;
        }
    
    
    

    while (part1) 
                                        
 {
     
       if (GAME_account>100)
       
       {
         part2=true;
       }
     
       else
           
    cout << "How much money do you want to put into your game account?"<<endl;
        cin >> GAME_account;
    
        
        if ((GAME_account>5000)|| (GAME_account<100) )                         
            
        {
        cout << "You can bet max 5000 and min 100" << endl;
            part1 = true;
            part2=false;
        }
        
        
        else
            
        {
            cout << "your account is loaded with," <<GAME_account <<endl;
            part1=false;
            part2= true;
            
        }
        
        
 }
        
        
     while (part2)
 { 

cout<< " We are now starting the game. The first one to win two times winns. To start the game you need to bet 100,300 or 500, how much do you want to bet?"<< endl;      

             
            cin>> GAME_bet;
         
            
          if ((GAME_bet == 500) || (GAME_bet == 300) || (GAME_bet == 100))     
            {
                part2=false;
                cout << "You choose to bet " << GAME_bet << endl;
                GAME_account = GAME_account - GAME_bet;
                cout << "You now have on your account " << GAME_account << endl;
                part3=true;
            }
            
            
            else
                
             {
           cout << "please pick to bet between 100, 300, 500" << endl;      
                part2 = true;
                part1=false;
                part3=false;
              }
         
      
      }
            
            while (del3)                       
              
     {
       cout<< "game begins"<<endl;
      cout<< "press ENTER to throw your first dice <<endl;               cin.get();                                                               
dice_one = rand() % 6 + 1;                                                                     
                cout << dice_one << endl;
                
                cout << "press ENTER to throw your second dice" << endl;
                cin.get();
                dice_two = rand() % 6 + 1;
                cout << dice_two << endl;
                cout << "Your numbers are now:" << endl;
                cout<< dice_one<< " and "<< dice_two<< endl;
                
                
    cout << "press ENTER to let the computer throw its first dice" << endl;
                cin.get();
                dice_three = rand() % 6 + 1;
                cout << dice_three << endl;
                
     cout << "press ENTER to let the computer throw its second dice" << endl;
                cin.get();
                dice_four = rand() % 6 + 1;
                cout << dice_four << endl;
                cout << "The computers numbers are now:" <<endl;
                cout<< dice_three<< " and "<< dice_four<< endl;

         
         
              if (MAXA(dice_three,dice_four)==MAX(dice_two,dice_one))
         
                
                    
                {
                  cout << "Oh, tie, lets start the round again" << endl;
                }
                
                
                
               else if (MAXA(dice_three,dice_four) <  MAX(dice_two,dice_one)) 
                   
                {
                    cout << "You won this round" << endl;
                    points_user++;                        
                }
                
                else
                    
                {
                    cout << "You lost this round" << endl;      
                    points_computer++;
                }
                
                
                cout << "Points are now: User-Computer" << endl;
                cout << points_user;
                cout << "-";
                cout << points_computer << endl;
                
         

                if (points_computer==2)
                    
              {                  
                    part3=false;
       cout << "im sorry, you lost better luck next time" << endl;
                    cout << "You now have left on your account " << endl;
                    cout << GAME_account;
                    
                  
               }

                else  if  (points_user == 2)
                    
                    part3=false;
                    cout << "you won the game with total" << endl;
                    cout << GAME_bet * 2 << endl;
                    cout << "You now have left :" << endl;
                    GAME_account = GAME_account + (GAME_bet * 2);    
                    cout<< GAME_account;
         
            
        }
         
         return 0;
          cin.ignore();  
     }


 


HAPPY FOR ANY HELP :)
Questions for you to consider.

Where does each while loop start and end? What happens when a while loop's condition is false?

My question.

Do you want the whole program to loop at the end, or just the sections you have in loops?

Please try to avoid unnecessary newlines. Your last else if before the return is useless. Can you figure out why?
Hi good questions to a beginner,

when a loops condition is false it stops the loop?
yes i do understand that the last else if is useless.

I want the whole program to get back to the start of the game, to the "welcome to the dice game" part. Not only the sections i have in loop.


I WOULD BE VERY HAPPY FOR ANY HELP SINCE IM A BIT STUCK.
You need to start the while loop before the start of the game and end it before return 0;

If you want the else if to work as I expect you want it to, you need to put brackets around the statements after it.

1
2
return 0;
cin.ignore();  //This will never be executed 
Last edited on
It looks as if you're missing a " on line 121.
Topic archived. No new replies allowed.