Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why the calculator is not working properly ?

+5 votes
asked Nov 27, 2021 by CPradhe (170 points)
#include <iostream>

using namespace std;

int

main ()

{

  char op;

  int a, b, c, d;

  cout << "if you are want to open calcutor then enter 1" << endl

    << "if you are want to find maximum number out of three then enter 2" <<

    endl << "if you are want to find odd-even then enter 3" << endl;

  cin >> d;

  switch (d)

    {

    case 1:

      cout << "entar first number" << endl;

      cin >> a;

      cout << "entar second number" << endl;

      cin >> b;

      cout << "enter the operation( +,-,*,/)" << endl;

      cin >> op;

      if (op = '/')

{

  cout << a << "/" << b << "=" << (float) a / b << endl;

}

      else if (op = '-')

{

  cout << a << "-" << b << "=" << a - b << endl;

}

      else if (op = '*')

{

  cout << a << "*" << b << "=" << a * b << endl;

}

      else if (op = '+')

{

  cout << a << "+" << b << "=" << a + b << endl;

}

      break;

    case 2:

      cout << "enter three number one by one" << endl;

      cin >> a >> b >> c;

      if (a > b)

{

  if (a > c)

    {

      cout << "maximum number is\t" << a << endl;

    }

  else

    {

      cout << "maximum number is\t" << c << endl;

    }

}

      else if (b > a)

{

  if (b > c)

    {

      cout << "maximum number is\t" << b << endl;

    }

  else

    {

      cout << "maximum number is\t" << c << endl;

    }

}

      break;

    case 3:

      cout << "enter any number" << endl;

      cin >> a;

      if (a % 2 == 0)

{

  cout << a << "\tis an even number" << endl;

}

      else

{

  cout << a << "\tis an odd number" << endl;

}

      break;

    default:

      cout << "invalid number";

    }

}

1 Answer

+1 vote
answered Nov 28, 2021 by Peter Minarik (84,720 points)

Assignment vs equality check

The = operator is the assignment operator. It assigns value to a variable. E.g.:

op = '/'

The == operator is the equality check operator. It checks if the left side and the right side are equal. E.g.

op == '/'

Also, before your finish your main function, you should set the return value as it has an int return type:

return 0;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...