how can i improve this

how can i improve this calculator?

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

using namespace std;

    int Amount;
	int Number;
	int whatoperation;
    int restart;

class operations
{
    public:

int add()
{
    int Total = 0;

    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)   // Keeps looping until i == Amount
	{
		cout << "Number " << i << ": " ;  // Shows user what number they are entering
		cin >> Number;

		Total = Number + Total;  // Adds number to total
	}

	cout << "The total is " << Total << endl;  // Prints out total value when loop has finished
	Total = 0;  // resets total back to zero

}

int subtract()
{
    int Total = 0;

    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total - Number;
		}
	}

	cout << "The total is " << Total << endl;
	Total = 0;
}

int multiply()
{
    int Total = 0;

    cout << "Enter in how many numbers you want to use" << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total * Number;
		}
	}
	cout << "The answer is " << Total << endl;
    Total = 0;
}

int divide()
{
    int Total = 0;

    cout << "Enter in how many numbers you want to use: " << endl;
	cin >> Amount;

	for(int i = 0; i < Amount; i++)
	{
		cout << "Number " << i << ": " ;
		cin >> Number;

		if(i == 0)
		{
			Total = Total + Number;
		}

		else
		{
			Total = Total / Number;
		}
	}
	cout << "The total is " << Total << endl;
	Total = 0;
}

int average()
{
    int Total = 0;


	cout << "Enter how many numbers you want to use: ";
	cin >> Amount;

	for(int i = 0; i < Amount; i++)
	{

        cout << "Number " << i << ": ";
	    cin >> Number;
        Total = Total + Number;
    }

    Total = Total / Amount;
	cout << "The average is " << Total << endl;
    Total = 0;
}

};

int main()
{

    operations callfunctions;

    int Total = 0;
	int Amount;
	int Number;
	int whatoperation;
	int restart;

	do{

    system("CLS");

	cout << "Here are your choices(enter the number before the operation)" << endl << endl;
	cout << "1.Addition" << endl;
	cout << "2.Subtraction" << endl;
	cout << "3.Multiplication" << endl;
	cout << "4.Division" << endl;
	cout << "5.Average Calculator" << endl;

	cin >> whatoperation;

	system("CLS");

	switch(whatoperation){
    case 1:
    callfunctions.add();
	break;

	case 2:
    callfunctions.subtract();
	break;

	case 3:
    callfunctions.multiply();
	break;

	case 4:
    callfunctions.divide();
	break;

	case 5:
    callfunctions.average();
	break;
	}
	cout << "Enter 1 to restart or 2 to quit: ";
	cin >> restart;


	}while(restart == 1);

    cin.get();
	return 0;
}

- Remove global variables; they are completely unnecessary.
- There is no need for the class called 'operations'.
- Give the user something user friendly, like '5 + 5 + 5 = (display total)'.

Those are just a few minor complaints. You could really simplify this and cut about 130 lines off of it (maybe even more). Focus on writing applications that your dog would be able to use and understand. Typing something like 5 * 5 is much better than having to select from a menu then enter the amount of number you would like to add.

I would usually complain about calling system system("CLS"), but I have found myself doing it before so I'll cut you some slack. Just remember, calling system leaves a huge security hole in your program, is very resourceful, and leaves your program 'grounded' to a specific OS.
Last edited on
how would i make it so they dont have to enter how many numbers they want to use
Make it so you can type:
9(1 + 6)

and the total is 63
Last edited on
how can i do that. do you know a tutorial?
?
Essentially, to be able to make calculations based on something like this:
9(1 + 6)

you'll have to parse the user's input to basically separate the numbers from the operators and translate them into something that your program can calculate an output for. You'll want to find tutorials about parsing/splitting strings for this. The amount of work for this really depends on how interpretive of human typing you want your calculator to be. Like, are these: 9 (1 + 6) and 9(1+6) valid inputs ? or will only this one work: 9*(1+6) ?

To start with, instead of using an integer like
whatoperation
you would start with accepting a full string as an input. Try it with the simplest case first like accepting only 2+3 from from the user into a string and then parse it and calculate then print the output " = 5".

Then eventually u could expand it to accept an arbitrary number of operations on the same line: 3/4 +6(1/4+2^3) - sqrt(4/(2pi))

As far as adding more operations to your calculator there's lots ! Like square root, exponents, logarithms, factorials, combinations/permutations - maybe even a prime number detector/factor'er'
Last edited on
Topic archived. No new replies allowed.