basic calculator
Mar 25, 2014 at 10:24pm UTC
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;
}
Mar 25, 2014 at 10:44pm UTC
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 Mar 25, 2014 at 10:46pm UTC
Mar 26, 2014 at 4:40pm UTC
so like
1 2 3
else {
cout << "This is not a valid symbol please try again" << endl;
}
?
Mar 26, 2014 at 5:02pm UTC
@Gabriels727
Just that! The most efficient way would be using an else
statement!
Mar 26, 2014 at 5:16pm UTC
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 Mar 26, 2014 at 5:18pm UTC
Mar 27, 2014 at 6:23pm UTC
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
}
Mar 28, 2014 at 2:24am UTC
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.
Mar 28, 2014 at 2:33am UTC
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 Mar 28, 2014 at 2:33am UTC
Mar 28, 2014 at 2:44am UTC
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 Mar 28, 2014 at 2:46am UTC
Mar 28, 2014 at 5:46am UTC
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.