Calculator help

#include <iostream>
#include <cmath>
using namespace std;
int array[50];
void elsef();

void add(){
cout << "Pick 2 numbers to add";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]+array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void subtract(){
cout << "Pick 2 numbers to subtract";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]-array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void multiply(){
cout << "Pick 2 numbers to multiply";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]*array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void divide(){
cout << "Pick 2 numbers to divide";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[2]/array[3];
cout << endl;
cout << array[4];
cout << endl;
}


int main(){

cout << "Press 1 for addition.\n" << "Press 2 for subtraction.\n"
<< "Press 3 for multiplication.\n" << "Press 4 for divison.\n";



cin >> array[1]; cout << endl;


if(array[1]==1)
{
add();
}


if(array[1]==2)
{
subtract();
}

if(array[1]==3)
{
multiply();
}

if(array[1]==4)
{
divide();
}

if(array[1]!=1,2,3,4){
elsef();
}

cout << "Press 6 to continue\nPress 7 to end the program\n";

cin >> array[7];
if(array[7]==6){
return main();
}

if(array[7]==7){
return 0;
}

else{
cout << "That is invalid\n";
return main();
}




}

void elsef(){
cout << "I'm sorry. That is invalid\n";
}

This code will always run the elsef function after one of the main 4. Can anyone help?
This is your code. Use the tags next time.
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
#include <iostream>
#include <cmath>
using namespace std;
int array[50];
void elsef();

void add(){
cout << "Pick 2 numbers to add";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]+array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void subtract(){
cout << "Pick 2 numbers to subtract";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]-array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void multiply(){
cout << "Pick 2 numbers to multiply";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]*array[2];
cout << endl;
cout << array[4];
cout << endl;
}

void divide(){
cout << "Pick 2 numbers to divide";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[2]/array[3];
cout << endl;
cout << array[4];
cout << endl;
}


int main(){

cout << "Press 1 for addition.\n" << "Press 2 for subtraction.\n"
<< "Press 3 for multiplication.\n" << "Press 4 for divison.\n";



cin >> array[1]; cout << endl;


if(array[1]==1)
{
add();
}


if(array[1]==2)
{
subtract();
}

if(array[1]==3)
{
multiply();
}

if(array[1]==4)
{
divide();
}

if(array[1]!=1,2,3,4){
elsef();
}

cout << "Press 6 to continue\nPress 7 to end the program\n";

cin >> array[7];
if(array[7]==6){
return main();
}

if(array[7]==7){
return 0;
}

else{
cout << "That is invalid\n";
return main();
}




}

void elsef(){
cout << "I'm sorry. That is invalid\n";
}


Try to verify those conditions that way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if(array[1]==1)
{
add();
}


else if(array[1]==2)
{
subtract();
}

else if(array[1]==3)
{
multiply();
}

else if(array[1]==4)
{
divide();
}

else{
elsef();
}


Last edited on
Topic archived. No new replies allowed.