I'm trying to write a program that mimics a calculator where the user inputs two integers and the operation to be performed. Then I want it to output the numbers, the operator, and the result. I need to use a switch statement and I'm very confused on what to do and I can't figure out how to do it. Any help would be greatly appreciated. Thanks

#include <iostream>

using namespace std;
int main()
{
int num1;
int num2;
int total;
int arithmeticOperator;

cout << "Enter any two integers: ";
cin >> num1 >> num2;
cout << "Enter an arithmetic operator: ";
cin >> arithmeticOperator;
cout << endl;

switch (arithmeticOperator)
{
case +:

case -:

case *:

case /:

}

return 0;
}

>int arithmeticOperator;
This should be:

char arithmeticOperator;

The reason is that cin>> is smart enough to tell what type of variable you're passing it. If arithmeticOperator is an int then entering any character other than a valid digit will cause the input stream to fail.

case +:

case -:

case *:

case /:

It's imperative that you remember to break each case of a switch statement, otherwise you'll end up executing every case after the one you want too. Also, unless you surround the characters with single quotes, the C++ parser will get confused:

case '+':
  // Add num1 and num2
  break;
case '-':
  // Subtract num1 and num2
  break;
case '*':
  // Multiply num1 and num2
  break;
case '/':
  // Divide num1 and num2
  break;

It's also a good idea to add a default case so that you can handle invalid operators:

case '+':
  // Add num1 and num2
  break;
case '-':
  // Subtract num1 and num2
  break;
case '*':
  // Multiply num1 and num2
  break;
case '/':
  // Divide num1 and num2
  break;
default:
  // Error
  break;

Aside from that, all you need to do is add the actual operations and print the result. :)

I found this calculator
Its kinda wierd but it works (sorta I made it crash beacuse I just that speacial)

#include <iostream>

int main(int argc, char* argv[])
{
   char exp [5];
   int x, y, z;
   bool e = false;
   cout << "X? ";
   cin >> x;
   cout << "Y? ";
   cin >> y;
   cout << "Z? ";
   cin >> z;
   int n1, n2;
   do {
      cout << "STATEMENT? ";
      cin >> exp;
      if (exp[0] == 'E')
      {
         e = true;
      }
      else
      {
         if (exp[2] == 'X')
            n1 = x;
         if (exp[2] == 'Y')
            n1 = y;
         if (exp[2] == 'Z')
            n1 = z;

         if (exp[4] == 'X')
            n2 = x;
         if (exp[4] == 'Y')
            n2 = y;
         if (exp[4] == 'Z')
            n2 = z;
      };
      cout << n1 + n2;
      cout << "\n";
   } while (e == false);
   return 0;
}

Does this look somewhat right? Also, my teacher said she thought I needed an if statement in the division case - if the denominator is 0. Not so sure how to do that?

#include <iostream>

using namespace std;
int main()
{
int num1;
int num2;
int total;
char arithmeticOperator;

cout << "Enter any two integers: ";
cin >> num1 >> num2;
cout << "Enter an arithmetic operator: ";
cin >> arithmeticOperator;
cout << endl;

switch (arithmeticOperator)
{
case '+':cout << "Num1 plus num2 ";
         break;

case '-':cout << "Num1 minus num2 ";
         break;

case '*':cout << "Num1 multiplied by num2 ";
         break;

case '/':cout << "Num1 divided by num2 ";
         break;

default:cout << "Error";
         break;

}

return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

>Does this look somewhat right?
Aside from saying what you're going to do without actually doing it, yes. You still haven't performed the operations, for example:

total = num1 + num2;

>my teacher said she thought I needed an if statement in the division case
That's generally a good idea because division by zero will throw an error. All you need to do is do the division if num2 is not zero and print an error otherwise:

if (num2 != 0)
  total = num1 / num2;
else
  cerr<<"Error: Division by zero"<<endl;
This article has been dead for over six months. Start a new discussion instead.