problem with functions

I'm trying to write a simple calculator program and here is the code.
the problem is when you choose an operation, after finishing the function it does not return to the main function, and instead the program chooses the first digit of the answer for the switch case


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

int main(){
	  class calculator {
	  public:
		  void multiply(){
			  int mfirst,msecond;
			  cout<<"Enter first number\n";
			  cin>>mfirst;
			  cout<<"Enter second number\n";
			  cin>>msecond;
			  cout<<mfirst*msecond<<"\n";
			  return;

		  }
	  	  void add(){
			int afirst,asecond;
			  cout<<"Enter first number\n";
			  cin>>afirst;
			  cout<<"Enter second number\n";
			  cin>>asecond;
			  cout<<afirst+asecond<<"\n";
			  return;
		  }
		  void subtract(){
			  int sfirst,ssecond;
			  cout<<"Enter first number\n";
			  cin>>sfirst;
			  cout<<"Enter second number\n";
			  cin>>ssecond;
			  cout<<sfirst*ssecond<<"\n";
			  return;
		  }
		  void divide(){
			  int dfirst,dsecond;
			  cout<<"Enter first number\n";
			  cin>>dfirst;
			  cout<<"Enter second number\n";
			  cin>>dsecond;
			  if (dfirst=0){
				  cout<<"Cannot divide by 0!";
			  }
			  else {
				  cout<<dfirst/dsecond<<"\n";
				  return;
				  
			  }
			  				  
			  }
			  
		  
		  

	};
	  calculator calc;
	  int choice;
		cout<<"Choose one! 1=Add 2=Divide 3=multiply 4=subtract\n";
		cin>>choice;
		switch (choice){
		case 1:calc.add();
			
		case 2:calc.divide();
			
        case 3:calc.multiply();
			
		case 4:calc.subtract();
			

		}
	getchar();
	
	return 0;
}
Last edited on
try this.
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
#include "stdafx.h"
 #include <iostream>
using namespace::std;

int main(){
	  class calculator {
	  public:
		  void multiply(){
			  int mfirst,msecond;
			  cout<<"Enter first number\n";
			  cin>>mfirst;
			  cout<<"Enter second number\n";
			  cin>>msecond;
			  cout<<mfirst*msecond<<"\n";
			  return;

		  }
	  	  void add(){
			int afirst,asecond;
			  cout<<"Enter first number\n";
			  cin>>afirst;
			  cout<<"Enter second number\n";
			  cin>>asecond;
			  cout<<afirst+asecond<<"\n";
			  return;
		  }
		  void subtract(){
			  int sfirst,ssecond;
			  cout<<"Enter first number\n";
			  cin>>sfirst;
			  cout<<"Enter second number\n";
			  cin>>ssecond;
			  cout<<sfirst*ssecond<<"\n";
			  return;
		  }
		  void divide(){
			  int dfirst,dsecond;
			  cout<<"Enter first number\n";
			  cin>>dfirst;
			  cout<<"Enter second number\n";
			  cin>>dsecond;
			  if (dfirst=0){
				  cout<<"Cannot divide by 0!";
			  }
			  else {
				  cout<<dfirst/dsecond<<"\n";
				  return;
				  
			  }
			  				  
			  }
			  
		  
		  

	};
	  calculator calc;
	  int choice;
		
	  char option = 'y';

	  while(option == 'y')
	  {
		cout<<"Choose one! 1=Add 2=Divide 3=multiply 4=subtract\n";
		cin>>choice;
		
		switch (choice)
		{
		case 1:
			calc.add();
		break;
			
		case 2:
			calc.divide();
		break;
			
        case 3:
		calc.multiply();
		break;
			
		case 4:
		calc.subtract();
		break;	

		}

		cout << "do you want to try again ?" << endl;
		cin >> option;
	  }
	getchar();
	
	return 0;
}
there's break; required after case is done
Topic archived. No new replies allowed.