Code problems

Hi I have created a very simple program that acts as a calculator but the while loop keeps running and the function will not work :\

If someone could please look at my coding to see if they can spot any problems i would be very grateful, I am just a beginner :L


Here is my 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
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

#include <iostream>
using namespace std;

int add();              // the add fucntion is prototyped 
int subtract();         // the subtract fucntion is prototyped
int multiply();         // the multiply fucntion is prototyped
int divide();             // the divide fucntion is prototyped

int DisplayMenu();      // the menu fucntion is prototyped
int MenuSelection();    // the menu selector fucntion is prototyped

int main ()
{
int exitcode;

while(exitcode !='9')
{

DisplayMenu();

exitcode = MenuSelection();
break;
}
}

int add() //Add function is defined
{
int a; 
int b;
int c;

cout<<"Enter the first number: "<<endl;
cin>>a;

cout<<"Enter the second number: "<<endl;
cin>>b;

c=a+b;

cout<<a<<"+"<<b<<"="<<c<<endl;

return 0;
}



int subtract() //Subtract function is defined
{

int a; 
int b;
int c;

cout<<"Enter the first number: "<<endl;
cin>>a;

cout<<"Enter the second number: "<<endl;
cin>>b;

c=a-b;

cout<<a<<"-"<<b<<"="<<c<<endl;

return 0;

}

int multiply() //Multiply function is defined
{

int a; 
int b;
int c;

cout<<"Enter the first number: "<<endl;
cin>>a;

cout<<"Enter the second number: "<<endl;
cin>>b;

c=a*b;

cout<<a<<"*"<<b<<"="<<c<<endl;

return 0;
}

int divide () //Divide function is defined
{

int a; 
int b;
int c;

cout<<"Enter the first number: "<<endl;
cin>>a;

cout<<"Enter the second number: "<<endl;
cin>>b;

c=a/b;

cout<<a<<"/"<<b<<"="<<c<<endl;

return 0;

}

int DisplayMenu() //Display menu function is defined
{

cout<<" CALCULATOR "<<endl;
cout<<" 1. Add"<<endl;
cout<<" 2. Subtract"<<endl;
cout<<" 3. Multiply"<<endl;
cout<<" 4. Divide"<<endl;
cout<<" 9. Exit program"<<endl;

}


int MenuSelection()
{
int selection;

cout<<"Please enter a menu choice"<<endl;

switch(selection)
{
	case '1': add();break;
	case '2': subtract();break;
	case '3': multiply();";break;
	case '4': divide();break;

}
return selection;
}

 
'9' is a character.

9 is the number 9.

while(exitcode !=9)
Last edited on
that'll help one thing is that my switch statement will not run my function
Same goes for the cases. Just "case 1" will do. Also, there's a random " in your code on line 133.
yea i was messing around with it I noticed that after I posted still any ideas on the functions not running!!! btw thanks guys :D
yea i was messing around with it I noticed that after I posted still any ideas on the functions not running!!!


As Gaminic said, because there's a difference between

case '1'

and

case 1
sorry for hasseling the functions now work but the menu has stopped working it run the menu then asks for add fucntions statments, once entered it repeats again?
Topic archived. No new replies allowed.