Division Problem

I am trying to make a simple math program, everything else works fine, the adding, subtracting, multiplication, but the division seems to crash quite a bit(I've marked the code where it crashes), and I'm not sure why that is. Any one care to help out? Thanks in advance, and I've also taken a look around the forums and FAQs but couldn't find an answer.

PS. I'm Using Dev-C++


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
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <iostream>
using namespace std;

int y;
int a, b, c, d;
int r, w;
int dt;

int main()
{
    st:
    srand((unsigned)time(0));      
    system("cls");
    cout << "Welcome to Math Helper\n";
    cout << "Input ''-1'' at any time to quit\n";
    cout << "Input ''-2'' at any time to return to the main menu\n";
    system("PAUSE");
    
    mm:
       r = 0;
       w = 0;             
       system("cls");
       cout << "Main Menu:\n";
       cout << "What would you like help with?\n";
       cout << "1 = Addition\n" ;
       cout << "2 = Subtraction\n";
       cout << "3 = Multiplication\n";
       cout << "4 = Division\n";
       cin >> y;
   
   switch (y)
      {
          case -1:
             goto end;  
          case 1:
             goto add;
          case 2:
             goto sub;
          case 3:
             goto mul;
          case 4:
             goto div;
          default:
             cout << "Please choose a valid number\n";
             system("PAUSE");
             goto mm;
      }      
   
   
   add:
      
      gna:
         a = (rand()%100);
         b = (rand()%100);
         c = (a + b);
         
         system("cls");
         cout << "Answer Stats: <Right: " << r << "> Wrong: <" << w << ">\n";
         cout << "Answer: " << a << " + " << b << " = ?\n";
         cin >> d;   
         
         if ( d == -2)
            {
                goto mm;   
            }
         
         else if ( d == -1)
            {
                goto end;   
            }
         
         else if (d == c)
            {
              cout << "Correct \n";
              system("PAUSE");
              r = (r + 1);
              goto gna;
            }        
         else
            {
                cout << "Incorrect \n";
                system("PAUSE");
                w = (w + 1);
                goto gna;
            }     
   sub:
      gns:
         a = (rand()%50);
         b = (rand()%50);
         c = (a - b);
         
         if (a < b | c < 0)
            {
                goto gns;
            }
         
         
         system("cls");
         cout << "Answer Stats: <Right: " << r << "> Wrong: <" << w << ">\n";
         cout << "Answer: " << a << " - " << b << " = ?\n";
         cin >> d;   
         
         if ( d == -2)
            {
                goto mm;   
            }
         
         else if ( d == -1)
            {
                goto end;   
            }
         
         else if (d == c)
            {
              cout << "Correct \n";
              system("PAUSE");
              r = (r + 1);
              goto gns;
            }        
         else
            {
                cout << "Incorrect \n";
                system("PAUSE");
                w = (w + 1);
                goto gns;
            }     
   
   
   mul:
      gnm:
         a = (rand()%12);
         b = (rand()%12);
         c = (a * b);
         
         system("cls");
         cout << "Answer Stats: <Right: " << r << "> Wrong: <" << w << ">\n";
         cout << "Answer: " << a << " * " << b << " = ?\n";
         cin >> d;   
         
         if ( d == -2)
            {
                goto mm;   
            }
         
         else if ( d == -1)
            {
                goto end;   
            }
         
         else if (d == c)
            {
              cout << "Correct \n";
              system("PAUSE");
              r = (r + 1);
              goto gnm;
            }        
         else
            {
                cout << "Incorrect \n";
                system("PAUSE");
                w = (w + 1);
                goto gnm;
            }     
   
   div://It crashes right here!! 
      gnd:
         a = (rand()%12);
         b = (rand()%12);
         c = (a / b);
         
         //This code tried to keep the math simple with no remainder
         dt = a % b;
         
         /*if (a < b | c < 0)
            {
                goto gnd;
            }
          */  
         if (dt != 0)
            {
                goto gnd;
            }
         
         
                 
         
         system("cls");
         cout << "Answer Stats: <Right: " << r << "> Wrong: <" << w << ">\n";
         cout << "Answer: " << a << " / " << b << " = ?\n";
         cin >> d;   
         
         if ( d == -2)
            {
                goto mm;   
            }
         
         else if ( d == -1)
            {
                goto end;   
            }
         
         else if (d == c)
            {
              cout << "Correct \n";
              system("PAUSE");
              r = (r + 1);
              goto gnd;
            }        
         else
            {
              cout << "Incorrect \n";
              system("PAUSE");
              w = (w + 1);
              goto gnd;
            }  
   
   end: 
      return 0;
}
My first comment would be AAAAARRRGGGHHHHHH goto's!!!!
Goto is generally regarded as a poor way to program, as it tends to lead to code that is difficult to maintain.
It is much better to use functions.

Back to your problem - well, 1 in 12 times b == 0, so you have a division by zero when calculating c. Add a test for b==0 before the division and either set b=1, or c=0 (depending on how you want to handle this case).

And get rid of the goto's... :-)

Fadrax:
Instead of testing if b == 0, why not making sure that b can't reach 0 in the first place?
b = 1 + rand%11

supness (OP):
I think the math for the division is too complicated. Why not multiply some random numbers first to make sure there never will be a remainder?

Also I would prefer functions and loops to get rid of the goto's like Faldrax already said, but that's your own choice. Most people will stop using these after a while automatically because it leads to spaghetti code.

Some joke:
When telling a programmer to: "go to heck", he is more offended by the "go to" than "heck"
Topic archived. No new replies allowed.