Need help with making a calculator using arrays.

I'm learning C++ atm., and I am at arrays right now. I understood the mechanics of it, but I was curious that if I could make something using them, to maybe understand them even more. So I started making a Calculator, using arrays, and I know there are many better ways to do it, but I wanted to challenge myself. Now, I got stuck here, I don't really know what I missed so I thought, I'll ask you guys, if maybe (I'm pretty sure yes) you can help me.

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


int main()
{
	
	int typeOfCalculation;
	
	
	cout << "Welcome to the Calculator v1.0!" << endl << "What would you like to do?" << endl << "1: Addition\n2: Subtraction\n3: Multiplation\n4: Division" << endl; //Calculation type
	
	cin >> typeOfCalculation;
	
	
	switch (typeOfCalculation) 
	{
		
		case 1:
			
			cout << "How many numbers do you want to add up?" << endl;
			
			
			int b;//index of array
			
			cin >> b; //how many numbers
			
			int arrAddition [b] = { 0 };//array
			int sum = 0;//sum of numbers
			
			
			cout << "Start typing the numbers one by one." << endl;
			
			
			for (int x = 0; x < b; x++)
			{
				cin >> arrAddition[x];
				
				sum = sum + arrAddition[x];		
			}
			
			cout << sum;		
	}
}


The thing is that it kinda quits the for loop, and just writes out "Start typing the numbers one by one." and the sum. But probably you'll paste it and see what it does.

How can I fix it?
your code worked fine when i pasted it into cpp.sh at http://cpp.sh/4yuwv

Welcome to the Calculator v1.0!
What would you like to do?
1: Addition
2: Subtraction
3: Multiplation
4: Division
1
How many numbers do you want to add up?
3
Start typing the numbers one by one.
32
32
32
96


Welcome to the Calculator v1.0!
What would you like to do?
1: Addition
2: Subtraction
3: Multiplation
4: Division
1
How many numbers do you want to add up?
3
Start typing the numbers one by one.
10 10 10
30
You can't use an array dynamically. You must use vector.

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


int main()
{

	int typeOfCalculation;


	cout << "Welcome to the Calculator v1.0!" << endl << "What would you like to do?" << endl << "1: Addition\n2: Subtraction\n3: Multiplation\n4: Division" << endl; //Calculation type

	cin >> typeOfCalculation;


	switch (typeOfCalculation)
	{

	case 1:

		cout << "How many numbers do you want to add up?" << endl;


		int b;//index of array

		cin >> b; //how many numbers

		vector<int> arrAddition;//array
		int sum = 0;//sum of numbers


		cout << "Start typing the numbers one by one." << endl;
		int i;

		for (int x = 0; x < b; x++)
		{
			cin >> i;
			arrAddition.push_back(i);

			sum = sum + arrAddition[x];
		}

		cout << sum;
	}

	system("pause");
	return 0;
}
#1: Yeah I tried it after I posted it,but in Dev-C++ it doesn't work.

#2: I don't really know vectors yet, but I'll try to do it, with your code it works, thanks!
I ended up with this, I guess it's fair enough, but I could've done better I guess.

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


int main()
{

	int typeOfCalculation;
	vector<int> arrAddition;
	vector<int> arrMultiplication;
	int sum = 0;//sum of numbers
	int outCome = 1;


	cout << "Welcome to the Calculator v1.0!" << endl << "What would you like to do?" << endl;
	CHOICE:cout << "1: Addition\n2: Subtraction\n3: Multiplication\n4: Division" << endl; //Calculation type

	cin >> typeOfCalculation;


	switch (typeOfCalculation)
	{

		case 1:

			cout << "How many numbers do you want to add up?" << endl;
	

			int b;//index of array

			cin >> b; //how many numbers

			cout << "Start typing the numbers one by one." << endl;
			int i;

			for (int x = 0; x < b; x++)
			{
				cin >> i;
				arrAddition.push_back(i);

				sum = sum + arrAddition[x];
			}

			cout << "\nThe Outcome is: " << sum << endl;
		break;
	
		case 2:
	
			int subtrahend;
			int minuend;
		
			cout << "What is the subtrahend?" << endl;
		
			cin >> subtrahend;
		
			cout << "What is the minuend?" << endl;
		
			cin >> minuend;
			
			sum = subtrahend - minuend;
			
			cout <<"\nThe Difference is: " << sum << endl;
		break;	
		
		case 3:
			
			cout << "How many numbers do you want to multiply?" << endl;
			
			
			int d;
			
			cin >> d;
			
			cout << "Start typing the numbers one by one." << endl;
			int k;
			
			for (int y = 0; y < d; y++)
			{
				cin >> k;
				arrMultiplication.push_back(k);
				
				outCome = outCome * arrMultiplication[y];
				
			}
			
			cout << "\nThe Outcome is: " << outCome << endl;
		break;
		
		case 4:
			
			int dividend;
			int divisor;
		
			cout << "What is the dividend?" << endl;
		
			cin >> dividend;
		
			cout << "What is the divisor?" << endl;
		
			cin >> divisor;
			
			sum = dividend / divisor;
			
			cout <<"\nThe Outcome is: " << sum << endl;	
		break;
		
		default:
			cout << "There is no such a possibility!\nPlease choose from the numbers above!" << endl;
			goto CHOICE;
	}
	

	return 0;
}
Topic archived. No new replies allowed.