Simple calculator operation

Hey guys so I'm trying to modify my old calculator program from previous assignments, into a calculator made of functions. What I'm struggling with is trying to make the calculator add the previous answer instead of the first number I put in. Thanks for helping!

Here's my header
1
2
3
4
5
6
7
8
9
10
#ifndef header
#define header

int		Divide (int, int);
int		Add (int, int);		
int		Mult (int, int);
int		Sub (int, int);


#endif 


Header cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int Add (int X, int Y)
	{
	return X + Y;
	}

int Mult (int X, int Y)
	{
	return X * Y;
	}

int Sub (int X, int Y)
	{
	return X - Y;
	}

int Divide (int X, int Y)
	{
	return (X / Y);
	}


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

using namespace std;

#include <stdlib.h>
#include "header.h"

void main ()

	{
		double		X;
		double		Y;
		double		Z;
		char		op;
		bool		vop;
	
		X = 0;
		Y = 0;
		Z = 0;
		vop = true;
	
			begin:
			cout << "Enter a whole number: ";
			cin >> X;
			Z = X;

			do
		{
			cout << "Enter operator (+-*/CcXx): ";
			cin >> op;

			if ((op != 'c' && 'C') && (op != 'X' && 'x'))

			switch (op)
			{
			case '+': 
				cout << "Enter another number: ";
				cin >> Y;
				Z = Add (X,Y);
				cout << "Total: " << Z << endl;
				vop = true;
				break;
					 
			case '-':
				cout << "Enter another number: ";
				cin >> Y;
				Z = Sub (X,Y);
				cout << "Total: " << Z << endl;
				vop = true;
				break;

			case '*':
				cout << "Enter another number: ";
				cin >> Y;
				Z = Mult (X,Y);
				cout << "Total: " << Z << endl;
				vop = true;
				break;

			case '/': 
				cout << "Enter another number: ";
				cin >> Y;
				Z = Divide (X,Y);
				cout << "Total: " << Z << endl;
				vop = true;
				break;

			case 'C': 	
			case 'c':
				cout << "Cleared" << endl;
				Z = 0;
				vop = true;
				goto begin;
				
				
			case 'X':	
			case 'x':
				cout << "Bye"<< endl;
				exit (0);

			default: 
				vop = false;
				cout << "Enter another operator" <<endl;
			
			
			}
		} while (vop != false);


}
Last edited on
closed account (Dy7SLyTq)
http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cpp
just thought you might want to see that for reference
I don't really understand the logic in some parts of your program:
- please explain what line 32 is supposed to achieve. If I choose c, the switch is not executed, but it is executed if I type C
- if you don't want to execute for c, why do you still treat the c case on line 69
- if you enter operator v, it will go to default, vop is false, prints "Enter another operator" then exits at line 87
- I REALLY hate goto statements. There might be some place for them, but you should avoid them. It makes code much harder to read

As for your question, if you really want to apply operator on the previous result, you need to replace Add(X,Y) with Add (Z,Y) and so on for Sub, Mult, and Divide
That site might be a little too complicated for me to understand sadly...
Thought I had to put line 32 in there.

Don't know why c isn't working...

Just noticed that problem too, going to try and fix that

I've been trying to figure out how to not use goto, but can't find a way to do that so far.

Thank you!
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
int main()
{
  double x=0., y=0., z=0.;
  char op;
  bool reset=true;

  do
  {
    if (reset)
    {
       cout << "Enter a number: ";
       cin >> x;
       reset=false;
    }
    else x=z;
   
    cout << "Enter operator (+-*/CcXx): ";
    cin >> op;

    switch (op)
    {
        case '+': 
	   cout << "Enter another number: ";
           cin >> y;
           z = Add (x,y);
           cout << "Total: " << z << endl;
           break;
					 
        case '-':
           cout << "Enter another number: ";
           cin >> y;
           z = Sub (x,y);
           cout << "Total: " << z << endl;
           break;

        case '*':
           cout << "Enter another number: ";
           cin >> y;
           z = Mult (x,y);
           cout << "Total: " << z << endl;
           break;

        case '/':
           cout << "Enter another number: ";
           cin >> y;
           z = Div (x,y);
           cout << "Total: " << z << endl;
           break;

        case 'C': 	
        case 'c':
              cout << "Cleared" << endl;
              reset=true;
              break;
				
				
        case 'X':	
        case 'x':
              op='x';
              break;

        default: 
             cout << "Invalid choice. Enter another operator" <<endl;
			
  } while (op!='x') ;
  cout << "Bye"<< endl;
}


I did not test it, so you might find some errors.
Last edited on
Topic archived. No new replies allowed.