Hey, i was just messing around and made a calculator and was just wondering if there was a way i could make it shorter? I'm not really bothered i just want to know?

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR 4");
    
    double firstNumber, secondNumber;
    char operation; 
    
    cout << "NOTE: YOU CAN USE X/x INSTEAD OF * FOR MULTIPLICATION!\n\n\n\n";  
    
    cout << "Please enter the first number : ";
    cin  >> firstNumber;
    
    cout << "\nWhich type of calculation would you like to use? : "; 
    cin  >> operation;
    
    cout << "\nPlease enter the second number : ";
    cin  >> secondNumber;
    
         
         switch(operation)
         {
                          case '+':
                          cout << "\nAnswer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                          cout << "\nAnswer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          cout << "\nAnswer = " << (firstNumber * secondNumber);
                          break;
                          
                          case 'x':
                          cout << "\nAnswer = " << (firstNumber * secondNumber);
                          break;
                          
                          case 'X':
                          cout << "\nAnswer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\nAnswer = " << (firstNumber / secondNumber);
                          break;
                          
         }
         
         system("PAUSE > nul");
}

Thanks.

>just wondering if there was a way i could make it shorter?
Yes, there are plenty of ways. Will they be an improvement? Probably not. The single best way to make your code shorter is to remove redundancy, but at the small length of your program, it's hard to remove redundancy and have it actually buy you anything.

>Will they be an improvement? Probably not

I am going to improve it yes. I am going to make it have more options, square root, etc etc

>I am going to improve it yes.
You misunderstand. I meant that the tricks I would use to shrink your code (as it stands) would make it shorter, but worse. Actually, looking more closely I see that you have quite a bit of redundancy in the multiplication cases. You can shrink those into one:

case '*':
case 'x':
case 'X':
  cout << "\nAnswer = " << (firstNumber * secondNumber);
  break;

>I am going to make it have more options, square root, etc etc
Unique code still has to be present if you want those features. If you want to avoid adding a lot of extra code as you increase the operations, try to abstract away most of the work. That way you can get more bang for each line of code.

Sorry for misunderstanding. Thanks for the multiplication classes, would it be "hard" to add sin, cos and tan?

>would it be "hard" to add sin, cos and tan?
Only in that you can't use a single character for your operation variable anymore.

If you want to keep using a switch, you could make a menu:

What do you want to do:
1. Multiply
2. ....
.....
8. Sin
9. Tan

input:

and then switch on the inputted number.

In your current program you should handle invalid input. If some enters a '^' for example, your program would just quit.

Niek

EDIT!!!

Got sqrt working!! WOO :)

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>
using namespace std;

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
    
    
    double firstNumber, secondNumber;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    
    cout << " ************************" << endl;
    cout << "  OPERATORS : +, -, *, /" << endl;
    cout << " *************************\n\n";
    
    cout << " ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;;
    cout << " ************************************************\n\n";
    
    cout << " Do you wish to use square root? Y, N : ";
    cin >> boolSqrt;
    
    if( boolSqrt == 'Y' || boolSqrt == 'y' )
    {
        cout << "\n\n ENTER NUMBER : ";
        cin  >> firstNumber;
        
        
                                  
        
        double result;
        
        result = sqrt (firstNumber);
        cout << "\n ANSWER = " << result;
                                 
                                  
   }    
      
    if( boolSqrt == 'N' || boolSqrt == 'n' )
    {
       
    cout << " ENTER FIRST NUMBER : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    cout << "\n ENTER SECOND NUMBER : ";
    cin  >> secondNumber;
    
         
         switch(operation)
         {
                          case '+':
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          default:
                          cout << "\n Wrong mathmatic operator!";
                          break;
                          
         }
         }
         
         else
         
         cout << "\n WRONG DECISION.";
         
         system("PAUSE > nul");
}

Can i ask a favor? When i choose to use square root, i input the number then it outputs my answer but after it it prints the case default? Can you help please :)!!!

When i choose to use square root, i input the number then it outputs my answer but after it it prints the case default?

It appears that it would print 'WRONG DECISION.' when using square root, because
effectively you have the following construct

if( boolSqrt == 'N' || boolSqrt == 'n' )
{
	<snip>
}
else
	cout << "\n WRONG DECISION.";
if ( boolSqrt != 'Y' || boolSqrt != 'y' && boolSqrt != 'N' ||
              boolSqrt != 'n')
         {
         cout << "\n WRONG DECISION.";
         }

Still doesn't work..?

Add one 'else' to glue the if statements together

if( boolSqrt == 'Y' || boolSqrt == 'y' )
{
    <snip>
}
[B]else[/B] if( boolSqrt == 'N' || boolSqrt == 'n' )
{
    <snip>
}
else
	cout << "\n WRONG DECISION.";

THANK YOU!

Now i'll move onto sin, cos and tan.

But would i do the same thing?

Like Y/N because what happens if they input two Y's for different things?

Because atm my program only takes 2 integers (or 1 for square rooting)

You could also ask for the operation first as I suggested and when you know what the user wants, ask for one or two integers

Thanks,

Ive changed code to case sqrt..

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>
using namespace std;

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
    
    
    double firstNumber, secondNumber, result;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    
    cout << " *****************************************" << endl;
    cout << "  OPERATORS : +, -, *, /, ! = SQUARE ROOT" << endl;
    cout << " *****************************************\n\n";
    
    cout << " ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;;
    cout << " ************************************************\n\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    result = sqrt (firstNumber);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                               cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n ANSWER = " << result;
                          break;
                          
                          default:
                          cout << "\n Wrong mathmatic operator!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }

Sounds good.

some like this:

Make your choice:
menu:

1. sqrt
2. sin
3. cos
4. multiply
5. add
6. etc....
cout <<"enter your choice:" << endl;
cin >> userinput;

if (userinput <= 3)
{
    cout << "enter one number" << endl;
    cin >> number1;
    switch(userinput )
    {
        case 1: //do sqrt
        case 2: //do sin
        //etc
    }
}
else
{
    cout << "enter number1" << endl;
    cin >> number1;
    cout << "enter number2" << endl;
    cin >> number2;
    switch(userinput )
    {
        case 4: //do multiply
       //etc
    }  
}

of course you would need to validate the input etc, but you get the idea

[edit]
Aha, you've changed your post, so my reply doesn't make sense anymore....

Thanks for the help, and yes i did change my post/code, Ive got to go now but when i return i'll add the options :) Thanks for all your help. I quite like my little program :)

EDIT: GOT SIN, COS, AND TAN IN!!

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

#define PI 3.14159265

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
    
    
    double firstNumber, secondNumber;
    double resultSqrt, resultSin, resultCos, resultTan;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    cout << "  OPERATORS : +, -, *, /, !, S, C, T" << endl;
    cout << " \n";
    
    cout << "  + = ADD\n  - = SUBTRACT\n  * = Multiplication\n  ";
    cout << "/ = Division\n  ! = SQAURE ROOT\n  S = SIN\n  ";
    cout << "C = COS\n  T = TAN\n";
    
    cout << " \n ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;
    cout << " ************************************************\n\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    resultSqrt = sqrt (firstNumber);
    
    resultSin = sin (firstNumber*PI/180);
    
    resultCos = cos (firstNumber*PI/180);
    
    resultTan = tan (firstNumber*PI/180);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                               cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n ANSWER = " << resultSqrt;
                          break;
                          
                          case 'S':
                          case 's':
                          cout << "\n ANSWER = " << resultSin;
                          break;
                          
                          case 'C':
                          case 'c':
                          cout << "\n ANSWER = " << resultCos;
                          break;
                          
                          case 'T':
                          case 't':
                          cout << "\n ANSWER = " << resultTan;
                          break;
                          
                          default:
                          cout << "\n WRONG MATHMATICAL OPERATOR!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }

Any reccomendations of what to add?

Any reccomendations of what to add?

Hmm, how about conversions, like from hex to dec/bin/oct and vice versa?

I'll try to add that :).

BTW, Do you think the layout is ok or should be changed a little?

BUMP..

Hey,

Just tried adding Binary and got some errors :S

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

#define PI 3.14159265

void binary(int);

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
    
    
    double firstNumber, secondNumber;
    double resultSqrt, resultSin, resultCos, resultTan, resultBinary;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    cout << "  OPERATORS : +, -, *, /, !, S, C, T, B" << endl;
    cout << " \n";
    
    cout << "  + = ADD\n  - = SUBTRACT\n  * = Multiplication\n  ";
    cout << "/ = Division\n  ! = SQAURE ROOT\n  S = SIN\n  ";
    cout << "C = COS\n  T = TAN\n  B = BINARY\n";
    
    cout << " \n ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;
    cout << " ************************************************\n\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    resultSqrt = sqrt (firstNumber);
    
    resultBinary = binary (firstNumber);
    
    resultSin = sin (firstNumber*PI/180);
    
    resultCos = cos (firstNumber*PI/180);
    
    resultTan = tan (firstNumber*PI/180);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                               cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n ANSWER = " << resultSqrt;
                          break;
                          
                          case 'S':
                          case 's':
                          cout << "\n ANSWER = " << resultSin;
                          break;
                          
                          case 'C':
                          case 'c':
                          cout << "\n ANSWER = " << resultCos;
                          break;
                          
                          case 'T':
                          case 't':
                          cout << "\n ANSWER = " << resultTan;
                          break;
                          
                          case 'B':
                          case 'b':
                          cout << "\n ANSWER = " << resultBinary;
                          break;
                          
                          default:
                          cout << "\n WRONG MATHMATICAL OPERATOR!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }
            
void binary(int number) 
{
     int remainder;
     
     if( number <= 1 )
     {
         cout << number;
         
         return;
     }
     
     remainder = number % 2;
     binary (number >> 1);
     cout << remainder;
     
}

LINE 45: Has the errors,

i got the code from

http://www.engin.umd.umich.edu/CIS/course.des/cis400/cpp/binary.html

PLEASE HELP

void binary(int) does not return anything, hence you cannot use it like you have done.
Without modifying the binary() function, you could use it like ...

case 'B':
case 'b':
cout << "\n ANSWER = "; binary (firstNumber);
break;

It is in one case..

No offense but thats spam..

I need to know the answer for binary not something i already have..

It is in one case..

No offense but thats spam..

I need to know the answer for binary not something i already have..

Spam? No spam here. You asked a question, mitrmkar pointed out the problem with your code, and showed you one way it could be made to work, or at least compile (there's another problem in there too), adding the qualifier "Without modifying the binary() function" to alert you that there are other options too. To make it work with your program you probably SHOULD modify your binary function.

If you think mitrmkar's response is spam, it just shows that you don't understand his response and how it could help you. A better response from you would be to thank him and ask for clarification or ask a follow-up question, not call him a spammer. Even if he had given you bad advice, which he didn't, it's totally unnecessary and rude, especially since he helped you earlier in this same thread.

Sorry about saying spam, what i meant was ive already got that, but now the posts edited, thank you very much. In your opinion is it worth adding binary?

Sorry about saying spam, what i meant was ive already got that, but now the posts edited, thank you very much. In your opinion is it worth adding binary?

Well, I guess I'll look for that "edited by" a little closer next time. The post that is up there now looked perfectly fine to me. No idea what it might have said before. You can always use "Reply with Quote" on things like that, so if it's edited, the original is still there.

To your "binary" question, it's up to you but heck, keep it. It's a good addition. See mitrmkar's current post. You'll have to decide whether you want to return a value or not and whether to display it in the main program or in the function itself. If you want to keep line 45 the way it is, you'll need to change the function to return a value. I imagine that value would be a string, not a double. resultBinary is a double so it probably shouldn't store the binary representation. If you want to keep the function the same, as a void function, you can't assign a value to it as you are doing in line 45. Just call the function.

Regardless, you are passing the function a double and binary conversions are generally done on integers.

Thank you all for all your help.

So people are mentioning editing the binary function, can you please show me how this could be done :S

Thank you all for all your help.

So people are mentioning editing the binary function, can you please show me how this could be done :S

Yes, reread my last post and mitrmkar's last post. One possible method, and the one I would use is as follows:

One, design binary so it takes an integer as a parameter (you already have this) and returns a string (you don't have this yet).

Two, pass binary an integer, not a double, in your function call. Currently you are passing it a double here:

resultBinary = binary (firstNumber);

C++ will actually do this double to integer conversion for you probably when you pass the variable to the function, but I like doing it myself:

int x = (int) firstNumber; // x is integer equivalent of firstNumber
resultBinary = binary (x);
cout << "Binary representation of " << x << " is " << resultBinary;

You'll get a warning, but ignore it. Since your function will return a string instead of integer, you need to change resultBinary from type double to type string.

Three, take all the cout statements out of your binary function. You're calculating the binary there, not displaying it. You are displaying it in main.

Four, you'll have to do some conversion from integers to characters/strings in your binary function. Research "typecasting". Also, this function, though it is not in the C++ standard, could come in handy. A lot of people stay away from the non-standard C++ functions, but it's up to you.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html

Hello,
I have decided to not use binary, i have added power and edited some of the set out,

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

#define PI 3.14159265

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
     
    double firstNumber, secondNumber;
    double resultSqrt, resultSin, resultCos, resultTan, resultPower;
    
    char operation, boolSqrt; 
    
    cout << "\n  OPERATORS : +, -, *, /, !, ^, S, C, T" << endl;
    cout << "\n";
    
    cout << "  + = ADD\n  - = SUBTRACT\n  * = Multiplication\n  ";
    cout << "/ = Division\n  ! = SQAURE ROOT\n  ^ = POWER\n  S = SIN\n  ";
    cout << "C = COS\n  T = TAN\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    resultSqrt = sqrt (firstNumber);        
    
    resultSin = sin (firstNumber*PI/180);
    
    resultCos = cos (firstNumber*PI/180);
    
    resultTan = tan (firstNumber*PI/180);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ADD : ";
                          cin  >> secondNumber;
                          cout << "\n EQUALS = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                          cout << "\n MINUS : ";
                          cin  >> secondNumber;
                          cout << "\n EQUALS = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n MULTIPLIED BY :  ";
                          cin  >> secondNumber;
                          cout << "\n EQUALS = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n DIVIDED BY :  ";
                          cin  >> secondNumber;
                          cout << "\n EQUALS = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n EQUALS = " << resultSqrt;
                          break;
                          
                          case '^':
                          cout << "\n TO THE POWER OF : ";
                          cin  >> secondNumber;
                          
                          resultPower = pow (firstNumber, secondNumber);
                          
                          cout << "\n EQUALS = " << resultPower;
                          break;
                          
                          case 'S':
                          case 's':
                          cout << "\n EQUALS = " << resultSin;
                          break;
                          
                          case 'C':
                          case 'c':
                          cout << "\n EQUALS = " << resultCos;
                          break;
                          
                          case 'T':
                          case 't':
                          cout << "\n EQUALS = " << resultTan;
                          break;
                          
                          default:
                          cout << "\n WRONG MATHEMATICAL OPERATOR!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }

I'm happy with my program atm, but I'm going to now make it handle 3-5 numbers, so could you guys help me if i got errors etc?

THANK YOU!

EDIT: CAN ANYONE THINK OF A BETTER SYMBOL FOR SQUARE ROOTING?
CAN ONLY BE 1 SYMBOL AND CAN'T BE S, SIN HAS IT.

MANY THANKS

This article has been dead for over six months. Start a new discussion instead.