How to make a simple Calculator?

This week I have to make a 4 function calcualtor ( + - / *)

But here is the catch...I have to enter a number, enter a operator, then enter another number. I can write the code for (num1, num2, operator) but thinking this one out is KILLING me..

I have the loop created for X/x to exit the program, and C/c to clear the calc...but how in the heck should I approach having a number, an operator, and then another number?


Here is what I have so far (psuedocode included that I'm still trying to code from)
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
#include <iostream>
using namespace std;
#include <stdlib.h>
void main ()

	{
//read first number

	long L1;
	long L2;
	long L3;


cout << "Enter a number ";
	cin >> L1 ;

//Read in the operator
// Is the operator valid?
		//yes - continue
		//no - go back and read operater again
// Is the operator X?
//		yes-stop
//		no - go on with program
//	Is operator C?
//		yes - (option 1 ) reset first # to 0, go get another operator
//		yes- (option 2) go read another first number
//		no - go on with program
// doing math
//		read second number
//		do math operation
//		Print result
//		Go read in another operator


char Op;
bool Valid;

//so were going to do a do-while loop..it always does the loop at least once

do{
	Valid = true;
cout << "Enter an operator (+ - / * C c X x): " ;
cin >> Op; //read in an operator
switch (Op)
	{
	
	case '+':
	case '-':
	case '*':	
	case '/':
			Valid = true;
			break;
	case 'X':
	case 'x':
			cout << " Terminiating Program " <<endl;
			exit(0); //this causes the program to immediatly terminate

	case 'C':
	case 'c':
		//we want C to clear the calc
		cout << "Clearing the Calculator " <<endl;
			Valid = false;
			break;
	default:
			Valid = false;

	}
	} while (!Valid);
	cout << "Enter another number" << endl;
	cin >> L2 ;




	}


Any hints? How do can I tie the L1 (Op) L2 together at the end? Its driving me nuts!

can't you just ask it directly after the operator?

that way you could do something like this (pseudo code):

1
2
3
4
5
6
7
8
9
switch(op)
{
case '+': cout<<"enter another number"<<"\n"; //note that using \n is less recource heavy
cin ...
...
break;
case '-' ....

}


or you could then write a function, so you won't have to retype this every time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Function()
{
cout<<"... ";
cin>>...
}
int main(){
switch(op)
{
case '+':
Function()
break;
case '-' ....

}
}


Or you could add a part in the function where it also finds the result.

But that all depends on weather or not you can ask it directly after you got the operator.

If you have any questions (or if its unclear) please ask :)

Cheers!


Edit: but coming back to the question, you could do something like: if (operator = '+' ) { result = number1+number2} so basically the same as you'd always do :) Using if loops in here should be easier anyway :)
Last edited on
Bah, its killing me! I cant figure out how to make it preform the operator AFTER the 2nd number is entered.

I want to be able to add two numbers together, then add another number to that sum..

5
+
2
=7
+2
=9
-5
=4

etc etc...how can I go about this best?

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
#include <iostream>
using namespace std;
#include <stdlib.h>
void main ()

	{
//read first number

	long L1;
	long L2;
	long L3;


cout << "Enter a number ";
	cin >> L1 ;

//Read in the operator
// Is the operator valid?
		//yes - continue
		//no - go back and read operater again
// Is the operator X?
//		yes-stop
//		no - go on with program
//	Is operator C?
//		yes - (option 1 ) reset first # to 0, go get another operator
//		yes- (option 2) go read another first number
//		no - go on with program
// doing math
//		read second number
//		do math operation
//		Print result
//		Go read in another operator


char Op;
bool Valid;

//so were going to do a do-while loop..it always does the loop at least once

do{
	Valid = true;
cout << "Enter an operator (+ - / * C c X x): " ;
cin >> Op; //read in an operator
switch (Op)
	{
	
	case '+':  cout<<"enter another number"<<endl;
	case '-':  cout<<"enter another number"<<endl;
	case '*':  cout<<"enter another number"<<endl;
	case '/':  cout<<"enter another number"<<endl;
			Valid = true;
			break;
	case 'X':
	case 'x':
			cout << " Terminiating Program " <<endl;
			exit(0); //this causes the program to immediatly terminate

	case 'C':
	case 'c':
		//we want C to clear the calc
		cout << "Clearing the Calculator " <<endl;
			Valid = false;
			break;
	default:
			Valid = false;
		
	}

cin >> L2 ;
 if (operator = '+' ) 
	 {cout<< L1 + L2 <<endl;}
 else { cout << "Help me" <<endl;
	}



	
	} while (!Valid);

 
	
	}
So create a loop that keeps modifying one number saved as a variable?
What??

I want it to operate just like a normal calculator.

5
+
2
=7
+3
=10
-8
=2

This is the part that is giving me a headache =[
Yep, I know. Here's a snippet of how you could do it (just with multiplying and an odd argument for while - just as an example):


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

int main ()

{
    using namespace std;

    int num = 5, newNum;
    char symbol;

    cout << "5";

    do
    {
        cout << "\n> ";
        cin >> symbol >> newNum;

        switch (symbol)
        {
            case '*':
                num = num*newNum;
                cout << "\n" << num;
                break;
        }
    } while (num < 100);

    return 0;
}

Last edited on
But isn't that just restricting the calculators function to <100? Meaning if I punch in;

99
+
5

i want it to read;

104 - so then I can
+5
=109
-100
=9

VERY nice do loop with a switch, I never even saw that.
But isn't that just restricting the calculators function to <100?


Well yes, it does do that, I just used that as an example. An option is to have a character to escape the loop. Here an example of that:

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

int main ()

{
    using namespace std;

    int num = 5, newNum;
    char symbol;

    cout << "5";

    do
    {
        cout << "\n> ";
        cin >> symbol >> newNum;

        switch (symbol)
        {
            case '*':
                num = num*newNum;
                cout << "\n" << num;
                break;
           case 'E': //falls through...
           case 'e': //falls here...
                break; //breaks
           default: //if anything else occurs
                break;
        }
    } while (symbol != 'E' || symbol != 'e'); //as long as it is not e, otherwise the loop is exited

    return 0;
}


Just noticed my mistake in arguments. Also added default.
Last edited on
So why are you int the Number 5?

I want to read in a number , so can I just replace the "int num" with the cin and store it as L1?

like

long L1

cout << "Enter a number" << endl;
cin >> L1;

then start the do loop?



Also, how can I add another loop where C/c clears the calc, and sends me back to the top of the cin part where I have to enter a #?
Last edited on
Like 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
#include <iostream>

int main ()

{
    using namespace std;

    int num, newNum;
    char symbol;

    do
    {
        cout << "Please enter a number. 0 to exit\n> ";
        cin >> num;
        if (num != 0)
        {

            do
            {
                cout << "\n> ";
                cin >> symbol;
                if (symbol != 'e' && symbol != 'E')
                {
                    cin >> newNum;

                    switch (symbol)
                    {
                        case '*':
                            num = num*newNum;
                            cout << "\n= " << num;
                            break;
                        default: //if anything else occurs
                            break;
                    }
                }
            } while (symbol != 'E' && symbol != 'e'); 
        }
    } while (num != 0);

    return 0;
}

GREAT!

see below for the edit's I have made.

But how can I replace num! = 0 to be x/X=exit the program? I was thinking stdlib.h and using exit?

C/c needs to clear the Calculator and return to the top, would I do a for loop here?


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

void main ()

{
    using namespace std;

    int num, newNum;
    char symbol;

    do
    {
        cout << "Please enter a number. 0 to exit\n> ";
        cin >> num;
        if (num != 0)
        {

            do
            {
                cout << "\n> ";
                cin >> symbol;
                if (symbol != 'e' && symbol != 'E')
                {
                    cin >> newNum;

                    switch (symbol)
                    {
                        case '*':
                            num = num*newNum;
                            cout << "\n= " << num;
                            break;
                     
                          

							case '-':
                            num = num-newNum;
                            cout << "\n= " << num;
                            break;
                   

							case '+':
                            num = num+newNum;
                            cout << "\n= " << num;
                            break;
                       


							case '/':
                            num = num / newNum;
                            cout << "\n= " << num;
                            break;

                        default: //if anything else occurs
                            break;


                    }
                }
            } while (symbol != 'E' && symbol != 'e'); 
        }
    } while (num != 0);

  }
Last edited on
Well because num is an int, typing in anything but a number would to do terrible, terrible things. That's why I used 0 instead of a char. You could other things to allow you to use a char, but unless you want to get into some complicated territory, 0 maybe your best bet. Who would want to do mathematical equations to 0 anyway? :)

I was thinking stdlib.h and using exit?


You could if you want. I wouldn't personally :)

C/c needs to clear the Calculator and return to the top, would I do a for loop here?


Doesn't it already do this?

Please enter a number. 0 to exit
> 5

> +5

= 10
> +5

= 15
> e
Please enter a number. 0 to exit
> 2

> +2

= 4
>


If you want to clear screen then I suggest to use:

cout << string(50, '\n');

wherever you want to clear the screen. 50 = the number of enters ('\n') occur. You can change both to whatever you want.

One thing you should do is make the user more aware of what to input. Even the first input is a bit confusing as to why you should input a number. Adding some more output would help greatly.
Last edited on
OK so now we are getting into realms of user not entering a number but gibberish chars instead, here's a little method you can use:

read stuff in as string.

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

bool isNumber(string str, int& i);

int main()
{
    int num;
    string userInput;

    cin >> userInput;
    if (isNumber(userInput, num))
    {
        ...
    }
    else
        cout << "Invalid number entered";

    cout << endl;
    return 0;
}

bool isNumber(string str, int& i)
{
    istringstream ss(str);
    return ss >> i;
}


but really you should be using double's because 34.2534 is a valid number for a calculator, but it's not an integer.

edit: If you get this advanced and decie to use it, don't forget to read up the API for istringstream//sstream, and make sure you add in the correct includes.
Last edited on
Topic archived. No new replies allowed.