Divide by zero workaround not working(basic C++ calc)

Sep 27, 2014 at 9:50am
Hi,

I just started learning C++ the day before yesterday. I am attemptinng to create a basic calculator based solely on my basic knowledge of ints, variables, if...else and boolean operators. I'm using Codeblocks as my IDE and the integrated GNU GCC compiler. Here's the code.

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
#include <iostream>

using namespace std;


int main()
{
  restart:
  int a,b,c,x;

  cout << "Welcome to the advanced arithmetic calculator.\nWhat do you want to do?\n1)Add\n2)Sub\n3)Mul\n4)Div\n\n";
  cin >> x;

  if(x==1||x==2||x==3||x==4)
  {
      cout << "\nEnter first number.\n\n";
      cin >> a;

      cout << "\nEnter second number.\n\n";
      cin >> b;

      if(x==1)
      {
        c=a+b;
      }

      else if(x==2)
      {
          c=a-b;
      }

      else if(x==3)
      {
          c=a*b;
      }
      else if(x==4 && b!=0)
      {
          c=a/b;

      }
      else if(x==4 && b==0)
      {
          cout << "\nUNDEFINED.\n\n";
          goto restart;
      }

      cout <<endl<< c <<" is the answer.\n\n";
  }
  else
  {
      cout << "\nERROR\n\n-------------\n\n";
      goto restart;
  }

  //-------------------------End of first calc

  cout <<"What would you like to do next? Would you like to 1)Restart or 2)Exit?\n\n";
  cin >> x;

  if(x==1)
  {
      goto restart;
  }

  else
  {
      cout << "\nBye.\n\n";
      return 0;
  }
}


You may have noticed my workaround for divide by zero:

1
2
3
4
5
6
7
8
9
10
 else if(x==4 && b!=0)
      {
          c=a/b;

      }
 else if(x==4 && b==0)
      {
          cout << "\nUNDEFINED.\n\n";
          goto restart;
      }


However, every time I run the program, attempting to divide by zero causes a crash and the program fails to return zero.

What am I doing wrong here?
Last edited on Sep 27, 2014 at 9:53am
Sep 27, 2014 at 10:01am
Your code should work. Did you rebuild the project after making changes?
Sep 27, 2014 at 11:11am
Yes. By default, I rebuild and execute.

P.S :Sorry for the late reply. I didn't expect it to be so prompt.
Last edited on Sep 27, 2014 at 11:11am
Sep 27, 2014 at 11:21am
Works fine: http://ideone.com/nNZkEf

Try to force rebuild manually. Make sure that you have proper project open (it is easy to modify current project, but compile previous).
Sep 27, 2014 at 11:33am
Nope. Should I try a different compiler??

In addition, code after //end of first calc------------------
is not being executed, even if i just do add, sub, mul or proper div.
Last edited on Sep 27, 2014 at 11:33am
Sep 27, 2014 at 11:34am
It returns zero however (see previous).
Sep 27, 2014 at 12:32pm
Copy your code. Create new project (close all subwindows and projects before). Paste your code. Check.
Sep 27, 2014 at 1:29pm
SOLVED. Praise thee, MiiNiPaa, who hath solved my problem. Thanks a ton. BTW, why did this help, exactly?
Sep 27, 2014 at 1:47pm
You messed up project settings. If you had more than one project open or created new one before problem arised, you might forget to change active one.
Sep 27, 2014 at 5:02pm
Use a switch statement for your menu of selecting the operation
Last edited on Sep 27, 2014 at 5:03pm
Topic archived. No new replies allowed.