Q:

What's wrong with cin >> num1, num2?

#include <iostream>
using namespace std;

int main() {
  char choice;
  int solution, num1, num2;

  cout << "Menu";
  cout << "\n========";
  cout << "\n+) Add";
  cout << "\n-) subtract";
  cout << "\n*) Multiplication";
  cout << "\n/) Division";
  cout << "\nx) Exit";
  cout << endl;
  cout << "\nEnter your choice: ";
  cin >> choice;

  switch (choice) {
    case '+':
      cout << "\nEnter the operands: ";
      cin >> num1, num2;
      solution = num1 + num2;
      cout << num1 << '+' << num2 << '=' << solution;
      break;
    case '-':
      cout << "\nEnter the operands: ";
      cin >> num1, num2;
      solution = num1 + num2;
      cout << num1 << '-' << num2 << '=' << solution;
      break;
    case '*':
      cout << "\nEnter the operands: ";
      cin >> num1, num2;
      solution = num1 * num2;
      cout << num1 << '*' << num2 << '=' << solution;
      break;
    case '/':
      cout << "\nEnter the operands: ";
      cin >> num1, num2;
      solution = num1 / num2;
      cout << num1 << '/' << (double)num2 << '=' << solution;
      break;
    case 'x':
    case 'X':
      cout << "\nExiting....";
      return 0;
      break;
    default:
      cout << "\nInvalid choice";
  }

  return 0;
}

So i am writing a program which allows the user to input an operation. Its pretty much a calculator! I must use a switch statement. I feel like I am very close but now i have hit a brick wall. After the user inouts an operation the program runs and says it to enter the operands it is then supposed to do the operation with the numbers entered and the displays them. Theere is some type of problem after the user inputs the 2 operands where the program just crashes. Any suggestions as to what it may be?

A:

cin >> num1, num2;

This syntax is not correct for what you want. To chain, use
cin >> num1 >> num2;

If you compile with warnings you'll get notified about this by the compiler
int a{}, b{};
std::cin >> a, b;

gives the error:
warning: right operand of comma operator has no effect [-Wunused-value]
std::cin >> a, b;

The whole statement is parsed as
((std::cin >> a), b);

Which consists of two comma-separated expressions. The b in that case doesn't have any effect. If you print the variables after the std::cin line above, you will always get 0 for b

A:

int solution, num1, num2;

This leaves all variables uninitialised. Trying to read from them without a previous assignment is undefined behaviour.

cin >> num1, num2;

The supposed intention of this line is to read into both variables. What happens really is an application of the comma operator, with operands cin >> num1 on the left side and num2 on the right side.
The left side is evaluated and a value is written to num1; the second operand has no effect and leaves num2 in its uninitialised status.
It's as if you had written cin >> num1;.

solution = num1 + num2;

The aforementioned undefined behaviour happens, rendering your entire program invalid.
You can fix the problem as follows:
cin >> num1;
cin >> num2;

