Making longer calculations in a calculator program

Hi guys!

I've made a basic calculator, and I'd like to improve it. One of the ways doing this is by making the calculator capable of doing more operations at once.
Right now it can only do 1+1 =, 2-4= etc... So only with 2 numbers.
So I have 2 questions:
- how could I make it capable of doing operations like 3+4+2 (so with more then 2 numbers), and maybe even mixed operations (like 3+4-2).
- could I make an ans function, so it'd be able to keep on running, using the previous awnser.

A part of my code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
case 1:
         cout << "****** Time to do some basic math!******" << endl;
         cout << "____________Addition____________________" << endl;
         cout << endl;
         cout << "Enter number 1: ";
         cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<endl;
         cout<<"The rounded result = "<<setprecision(3)<<result<<endl;
		 break;


And if you're awnsering, could you please explain why it should be done that way? I'm quite noob =)

Thanks in advance!
Keep it simple. Something like this, using your above code as a reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//...CODE CODE CODE

while(num1 == '+')
{
  num2 = result
  cin >> num1;
  result = result + num1;
  cout << endl << num2 << ' ' << user << ' ' << num1 << " = " << result;
  cin >> num1;
}

//...CODE CODE CODE


This uses the variable you already have in a loop that executes as long as the user inputs a '+' sign for num1. There are probably a few things wrong with it but this was just to give you a general idea.
Last edited on
I added this (a bit moderated), but then it's never able to leave the loop.. So how should I make it leave the loop?
Enter something for num1 that isn't the '+' character. Like I said there are probably a few things wrong with this but it was a demonstration not a litereal "Here copy and paste this".
I know, thats why I said I moderated a bit..
But nvm I found the sollution :)

Thanks!
Get into the habbit of commenting your code, so if one day somebody has to modify your code, they'll know what they're looking at.
maybe what you need is this:

 
int main (int argc, char * argv[])


in your main function.

btw, my lame (actually, it's somebody's code :) ) code is more or less 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
#include <iostream>
#include <conio.h>
using namespace std;

void simple_calc (int n, char p, int m) {
	switch (p) {
	case '+': {
		//do your job here...
		break;
	}

	case '_': {
		//do your job here...
		break;
	}

	//and so on (till the last case

	default: break;
	}
}

int main () {
	int num1, num2;
	char sign;
	cout << "enter the number: ";
	cin >> num1 >> sign >> num2;
	simple_calc (num1, sign, num2);
	cout << "press any key to continue..."
	getch();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.