How to Make a Simple Calculator

This is for the noobies. But the calc. has all 4 operations.
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
  #include <iostream>
using namespace std;
int main()
{
 double fnum, snum, op, answer;
 top:
 cout << "\n\nWelcome to EZ Use Calculator." << endl;
 cout << "Below is a list of different operations." << endl;
 cout << "1 - Addition";
 cout << "\n2 - Subtraction";
 cout << "\n3 - Multiplication";
 cout << "\n4 - Division";
 cout << "\nWhat operation would you like to commence?:";
 cin >> op;
 if (op == 1) goto addop;
 else if (op == 2) goto subop;
 else if (op == 3) goto mulop;
 else if (op == 4) goto divop;
 else goto errop;
 addop:
     cout << "Enter the first number:";
     cin >> fnum;
     cout << fnum << " + ";
     cin >> snum;
     answer = fnum + snum;
     cout << fnum << " + " << snum << " = " << answer;
     goto top;
 subop:
     cout << "Enter the first number:";
     cin >> fnum;
     cout << fnum << " - ";
     cin >> snum;
     answer = fnum - snum;
     cout << fnum << " - " << snum << " = " << answer;
     goto top;
 mulop:
     cout << "Enter the first number:";
     cin >> fnum;
     cout << fnum << " X ";
     cin >> snum;
     answer = fnum * snum;
     cout << fnum << " X " << snum << " = " << answer;
     goto top;
 divop:
     cout << "Enter the first number:";
     cin >> fnum;
     cout << fnum << " / ";
     cin >> snum;
     answer = fnum / snum;
     cout << fnum << " / " << snum << " = " << answer;
     goto top;
 errop:
    cout << "\nThe operation you wish for is unavailable.";
    goto top;
 return 0;
}
Last edited on
this is good for a beginner, but if you want to learn some more, here are some suggestions... learn switches, functions and classes, which will make it easier to understand and write, and are important things to learn anyways. functions especially since almost all languages support them. most languages support classes and switches too. i would then learn how to parse a reverse polish notation calculator and then output the answer. finally i would work my way up to writing something like bajarne stroustrops infamous calculator compiler.
I know about those, I literally just learnt it today. But the switches can only be used with integer types. Since this calculator is used for ALL numbers, I didn't use the switch loop. Thanks for the suggestion though. I do hope to become a great programmer some day. :)
switches can be used on any constant, not just int, and its not a loop (well i guess it could be but normally it shouldnt be)
Topic archived. No new replies allowed.