You are declaring new variables inside a case statement without creating an enclosing scope:
switch (choice)
{
case 1: get_two_numbers(x, y);
//* vv here vv *
int sum = add(x, y);
//* ^^ here ^^ */
cout << x << " + " << y << " = " << sum << endl;
break;
case 2: get_two_numbers(x, y);
//* vv here vv */
int diff = subtract(x, y);
//* ^^ here ^^ */
cout << x << " - " << y << " = " << diff << endl;
break;
default:;
}
Here's how to fix it:
switch (choice)
{
case 1:
{
get_two_numbers(x, y);
int sum = add(x, y);
cout << x << " + " << y << " = " << sum << endl;
}
break;
case 2:
{
get_two_numbers(x, y);
int diff = subtract(x, y);
cout << x << " - " << y << " = " << diff << endl;
}
break;
default:
break;
}
Of course, the exact formatting of brackets and indentation is up to you.
A "case" of a switch doesn't create a scope, so, as the error says, you're jumping over the initialization of "sum" if the choice isn't 1.
You either need to declare sum and diff outside the switch, or create blocks with { } for each of the cases.