How do I stop in the middle of an if statement

I'm trying to make a game of chutes and ladders and i want to be able to get out of the big if statement. How do i do it?



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
#include<iostream>
#include<string>						
#include<ctime>
using namespace std;

void Spin(int&);          			        	// The void function "Spin" assigns the variable "spinner" a number

void Spin(int& spinner) 	                		//  Precondition:
{								//     "spinner" equals zero
  srand(time(NULL));						//  Postcondition:		
  spinner=rand()%6+1;						//     "spinner" is assigned a random number between 1-6
}


int main()
{
   int space[6];						// "space" keeps track of what space all the players are on the board
   int number;							// "number" is the amount of players that are playing
   int i=0;							// "i"
   int spinner;							// "spinner" this gets a random nuber 1-6 and adds it to space
   int win;							// "win" decides whether the program should continue or not
   string answer;						// "answer" has the user input something to continue
   cout<<"Enter how many players you want."<<endl;
   cin>>number;							// This intializes all the dimensions of space as zero
   for (i=0;i<number+1;i++)
   {
    space[i]=0;
   }

  cout<<"Everone starts on space 0."<<endl;

   win=1;

   while(win==1)						// This loop keeps going until someone wins
   {
      for (i=1;i<=number;i++)            			//This gives each of the players a turn
      {
        if (space[1]<100||space[2]<100||space[3]<100||space[4]<100||space[5]<100)		// After someone wins it stops the next player(s) by going
        {
          cout<<"Player "<<i<<" type something in to continue."<<endl;
          cin>>answer;						//This pauses the program so its not all done in one second
 

           Spin(spinner);					//This calls to the Spin function


          space[i]=space[i]+spinner;				//Adds what the player rolled to their space
          cout<<"Player "<<i<<" spun a "<<spinner<<". "<<endl;
          if(space[i]>100)
          {
          space[i]=100;
          }
          cout<<"You are now on space "<<space[i]<<"."<<endl;

/********************************************************************************/
/*										*/
/*      These if statements are the "chutes" and "ladders" of the program	*/
/*										*/
/********************************************************************************/

          if (space[i]==4)
          {
              cout<<"You donated blood. You advance to space 14."<<endl;
              space[i]=14;
          }
          if (space[i]==9)
          {  
              cout<<"You won the pie-eating contest. You advance to space 31."<<endl;
              space[i]=31;
          } 
          if (space[i]==16)
          {  
              cout<<"You knocked a bookcase over. You go back to space 6."<<endl;
              space[i]=6;
          } 
          if (space[i]==21)
          {  
              cout<<"You won the pet show. You advance to space 42."<<endl;
              space[i]=42;
          } 
          if (space[i]==28)
          {  
              cout<<"You won the lottery! You advance to space 84."<<endl;
              space[i]=84;
           } 
          if (space[i]==36)
          {  
              cout<<"You baked a cake for your mother. You advance to space 44."<<endl;
              space[i]=44;
          } 
          if (space[i]==47)
          {  
              cout<<"You lost your favorite book. You go back to space 26."<<endl;
              space[i]=26;
          } 
          if (space[i]==49)
          {  
              cout<<"You forgot your mother's birthday. You go back to space 11."<<endl;
              space[i]=11;
          }
          if (space[i]==51)
          {
              cout<<"You helped a cat get out of a tree. You advance to space 67."<<endl;
              space[i]=67;
          }
          if (space[i]==56)
          {
              cout<<"You didn't feed your pet. You go back to space 53."<<endl;
              space[i]=53;
          }
          if (space[i]==62)
          {
              cout<<"You failed a big test. You go back to space 19."<<endl;
              space[i]=19;
          }
          if (space[i]==64)
          {
              cout<<"You lost your pet. You go back to space 60."<<endl;
              space[i]=60;
          }
          if (space[i]==71)
          {
              cout<<"You got an 'A' on a big test. You advance to space 91."<<endl;
              space[i]=91;
          }
          if (space[i]==80)
          {
              cout<<"You have discovered a cure for cancer. You advance to space 100."<<endl;
              space[i]=100;
          }
          if (space[i]==87)
          {
              cout<<"You crashed your parents' car. You go back to space 24."<<endl;
              space[i]=24;
          }
          if (space[i]==93)
          {
              cout<<"You broke the TV. You go back to space 73."<<endl;
              space[i]=73;
          }
          if (space[i]==95)
          {
              cout<<"You spilled the paint on the floor. You go back to space 75."<<endl;
              space[i]=75;
          }
          if (space[i]==98)
          {
              cout<<"You caught the flu. You go back to space 78."<<endl;
              space[i]=79;
           }
          if (space[i]>=100)
          {
              cout<<"Player "<<i<<" YOU WIN!"<<endl;
              win=2; 
          }
         if (space[i]>=100)
         {
             space[i]=100;
         }
        }							// Ends the if statement
       }							// Ends the for statement
     }								// Ends the while statement
   return 0;
   }								// Ends int main 

Right off, break;, is what I think of. Second, may want to consider a switch statement (possibly) in place of the multiple if statements from line 61 to 159.
Last edited on by closed account z6A9GNh0
break; will not remove you from an if statement, it only works for loops and switch statements. If you have a giant if statement that you would need to break out of without executing the whole thing, chances are you need to re-work your program.
break will get you out of the for loop, which would seem like a logical thing to do when you hit line 154. Then you could get rid of the big ugly if (space[x]...) conditional.



Your question is a bit unclear. There is no way to stop in the middle of an if statement.

But I guess you want to avoid performing all the other tests after the one that succeeds. Here, as you are in a for loop, you could use continue;. But you'd need to add it into all your if cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
          if (space[i]==4)
          {
              cout<<"You donated blood. You advance to space 14."<<endl;
              space[i]=14;
              continue;
          }
          if (space[i]==9)
          {  
              cout<<"You won the pie-eating contest. You advance to space 31."<<endl;
              space[i]=31;
              continue;
          } 
          if (space[i]==16)
          {  
              cout<<"You knocked a bookcase over. You go back to space 6."<<endl;
              space[i]=6;
              continue;
          }

          ...


But I think BHXSpecter's switch statement solution would be cleaner, as you're testing the same variable to see it has one out of a set of values.

If you were testing just 3 or 4 values, you could stick with the if tests. But all bar the first should be an else if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
          if (space[i]==4)
          {
              cout<<"You donated blood. You advance to space 14."<<endl;
              space[i]=14;
          }
          else if (space[i]==9)
          {  
              cout<<"You won the pie-eating contest. You advance to space 31."<<endl;
              space[i]=31;
          } 
          else if (space[i]==16)
          {  
              cout<<"You knocked a bookcase over. You go back to space 6."<<endl;
              space[i]=6;
          }
          else
          {
          }


You might also consider factoring the ifs/switch for the special squares out into its own function?

Andy

PS You need to move srand() out of Spin() to the start of main(). It only needs to be called once during the lifetime of a program.
Last edited on
Well if his if statements are nested in a while loop they both work, but as you said it would jump out of the while loop break;. I was thinking of continue; but again they both have to be in the if statements if they are in a while loop.
Simple, Make different goto point's in your code, and call them as you wish. Remember when you declare a goto point, the place in the code that you make it in actually matter's. Or you can manipulate continue;,breakandexit

If you have no idea how to do so, then read the article in this website on control structures.
Last edited on
I have always thought goto to be poor coding habit and should be avoided if at all possible.
I have always thought goto to be poor coding habit and should be avoided if at all possible.


Agreed. If you need a goto, you are probably doing something wrong.
In that case good luck because you are probably going to need various while loop's that you can break;orcontinue; instead of the if statements. But that is just my opinion i'm not sure. Ill see what I come up with.

Ps. I don't get it, for what reason and where do you need to break a if statement?
Last edited on
Unless I'm misunderstanding the problem. I think converting all the if statements to one switch statement will do what he is wanting to do.
What if you have a do { } while(0); ? You will be able to break; then.
Like this:
1
2
3
4
5
6
7
do {
  if( .... ) // All your IFs
  {
     ....
     break;
  }
} while(0);
Instead of using sequence of if statements why don't you use else if in place of each if except first.

In this case whenever you hit any of the condition the control will get out of the sequence of if comparision.

In your current code only one condition will satisfy in any case so you should write your code like

1
2
3
4
5
if (//some condition )

else if(//other condition )

else if(//next condition ) 


and so ....
"goto" enough.

There is always one reason for the existing of everything, just like "goto", hehe.
Topic archived. No new replies allowed.