basic calculator

This calculator works and all, but how do I make is shorter?

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

using namespace std;

float num1, num2, result;
char symbol;

int main()
{
	while (1)
	{
		system("CLS");
		cin.clear();

		cout << "Please enter the first number: ";
		cin >> num1;
		cout << "what you want to do with these numbers (+, -, *, /): ";
		cin >> symbol;
		cout << "Please enter the second number: ";
		cin >> num2;
		if (symbol == '+')
		{
			result = num1 + num2;
			cout << num1 << "+" << num2 << "=" << result << endl;
		}
		if (symbol == '-')
		{
			result = num1 - num2;
			cout << num1 << "-" << num2 << "=" << result << endl;
		}
		if (symbol == '*')
		{
			result = num1 * num2;
			cout << num1 << "*" << num2 << "=" << result << endl;
		}
		if (symbol == '/')
		{
			result = num1 / num2;
			cout << num1 << "/" << num2 << "=" << result << endl;
		}
		system("PAUSE");
	}
	return 0;
}
Move the cout outside the if statements. You only need it once.
1
2
3
4
5
6
7
8
9
    if (symbol == '+')
        result = num1 + num2;
    if (symbol == '-')
        result = num1 - num2;
    if (symbol == '*')
        result = num1 * num2;
    if (symbol == '/')
        result = num1 / num2;
    cout << num1 << symbol << num2 << "=" << result << endl;


BTW, what happens if the symbol is not one of the four?
You need some error handling.

A switch statement, although not shorter, will accomplish the same thing and give you the opportunity to catch symbols which are not one of the four.
Last edited on
so like

1
2
3
else{
    cout << "This is not a valid symbol please try again" << endl;
}

?
@Gabriels727
Just that! The most efficient way would be using an else statement!
ok and if the person doesn't input a number where it asks for a number how do i say taht this isnt a number? so i do

1
2
3
4
if((num1 != float) || (num2 != float))
{
     cout << "this is not a valid number." << endl;
}

?

infact i tried that and it was wrong, so what do i do?
Last edited on
When cin gets input it can't use, it sets failbit:
1
2
3
4
5
6
int n;
cin >> n;
if(!cin) // or if(cin.fail())
{
    // user didn't input a number
}
Well.. I have an idea. How about making functions?
Functions can seriously shorten your code.
Except that you have to implement them separately.

But really useful. Also, functions are portable.

closed account (N36fSL3A)
ProgrammerJames wrote:
Well.. I have an idea. How about making functions?
Functions can seriously shorten your code.
Except that you have to implement them separately.

But really useful. Also, functions are portable.
Well functions are pretty much overkill in this scenario.
Last edited on
1
2
3
4
5
6
7
8
9
10
switch(symbol)
{
    case '+': result = num1 + num2; break;
    case '-': result = num1 - num2; break;
    case '*': result = num1 * num2; break;
    case '/': result = num2 ? num1 / num2 : 0; break;
    default: std::cerr << "Invalid operator." << std::endl;
}

std::cout << num1 << ' ' <<  symbol << num2 << " = " << result << std::endl;
Last edited on
Well functions are pretty much overkill in this scenario.


I reckoned. I am just trying to give some ideas. :)
Topic archived. No new replies allowed.