I have the following error in my Calculator code and do not understand how to correct it. Please any advice would be helpful.
ERROR: error: jump to case label [-fpermissive]| error:crosses initialization of 'int sum'| error: 'exit' was not declared in this scope|
CODE:
#include <iostream> #include <cmath> using namespace std; void display_menu(); int get_menu_choice(); void get_two_numbers(int &a, int &b); int add(int a, int b); int subtract(int a, int b); int main() { int choice; do { display_menu(); choice = get_menu_choice(); int x, y; 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:; } } while (choice != 3); cout << "Good bye...now." << endl; return 0; } void display_menu() { cout << endl; cout << "Simple Calculator Menu" << endl; cout << "----------------------" << endl; cout << " 1. Addition (+) " << endl; cout << " 2. Subtraction (-) " << endl; cout << " 3. Quit to exit the program" << endl; cout << endl; } int get_menu_choice() { int choice; cout << "Enter your selection (1, 2, or 3): "; cin >> choice; while(((choice < 1) || (choice > 3)) && (!cin.fail())) { cout << "Try again (1, 2, or 3): "; cin >> choice; } if (cin.fail()) { cout << "Error: exiting now ... " << endl; exit(1); } return choice; } void get_two_numbers(int &a, int &b) { cout << "Enter two integer numbers: "; cin >> a >> b; } int add(int a, int b) { return (a + b); } int subtract(int a, int b) { return (a - b); }
The problem is that variables declared in one case are still visible in the subsequent case s unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case .
Consider this code: switch(k) { case 1: int t = 4; break; default: break; } It will cause a "crosses initialization" error, because it is possible to skip the initialization of t, but after that it will still be in scope, even though it was never created in the first place.
A case or default label can only appear inside a switch statement. The constant-expression in each case label is converted to a constant value that's the same type as condition . Then, it's compared with condition for equality.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With